Wednesday, December 9, 2009

POST data using cURL

This is about posting some data to a page via cURL. We already know the other and common method to post data, i.e. using a form and setting its method to POST. Sometimes we need to POST data using php without any form. So for this we can use cURL. 


Scenario: In our case we will send a user id along with a status and a message to our other page to process it and return it's result. We assume that we have a function called currentUserId() which will give us ID of current user and we will set status to 1. 


Explanation with code:

First we will get the ID of the user. We may use any method set the variable.

$id = currentUserId(); ////get user id
$status = 1; ///just a variable
$msg = 'Hello! I am here.';
 Now we will make an array of the fields which we are going to send
$fields = array(
    'id'=>$id,
    'status'=>$status, 
'msg'=>urlencode($msg)
);

Using urlencode will help to avoid problems because of html tags and stuff. Do include this when sending strings. It will convert spaces and other entities to url format like space is represented as %20 etc..

Now, to send data to external source we will need to convert our data to query string format. In query string, each different variable is separated by & sign and having 2 entities each, 1 for variable name and other for its value. In our case it should be like id=123&status=1&msg=Hello!%20I%20am%20here.
So for this we will use a foreach loop to convert our fields to query string.
foreach($fields as $key=>$value)  

    $fields_string .= $key.'='.$value.'&';  ///all elements to send with & sign in between as standard of query string
}  
rtrim($fields_string,'&'); ////remove extra & sign at the end ,,, we can use substr too


Now we will initiate the connection for curl and set options like url, number of vars and vars.

$con = curl_init(); //set options url, number of POST vars, POST vars  
curl_setopt($con ,CURLOPT_URL,$url);  
curl_setopt($con ,CURLOPT_POST,count($fields));  
curl_setopt($con ,CURLOPT_POSTFIELDS,$fields_string);

Now to send the vars, we need to execute curl using following function
$response= curl_exec($con);
The response from the page will in $response.

At the end we will close the connection.
curl_close($con);
COMPLETE CODE:::

$id = currentUserId(); ////get user id
$status = 1;
$msg = 'Hello! I am here.';  
$url = 'http://example.com/process-post.php'; ///URL where we are sending our data 
$fields = array(
    'id'=>$id,  
    'status'=>$status,  
    'msg'=>urlencode($msg)  
);  
//making query string for POST 
foreach($fields as $key=>$value) 
$fields_string .= $key.'='.$value.'&';  ///all elements to send with & sign in between as standard of query string
rtrim($fields_string,'&'); ////remove extra & sign at the end  
//initiate connection 
$con = curl_init();  
//set options url, number of POST vars, POST vars 
curl_setopt($con ,CURLOPT_URL,$url); 
curl_setopt($con ,CURLOPT_POST,count($fields)); 
curl_setopt($con ,CURLOPT_POSTFIELDS,$fields_string);  
//execute curl
$response= curl_exec($con);  
//close connection 
curl_close($con);

No comments:

Post a Comment

Developer Instincts