Wednesday, July 10, 2013

Use cUrl in PHP to do posting to another web page

I have a registration form in which i need to validate user's input before send the data to third-party web page for transaction, and then returns back to the current page so that i can record the data and transaction into my database. To do this, i used cUrl which post the information collected from the user to the third-party  web page (as the third party web page requires post action). The way to do this is very simple as follows:

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,"");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,

// in real life you should use something like:
// curl_setopt($ch, CURLOPT_POSTFIELDS, 
//          http_build_query(array('postvar1' => 'value1')));

// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$server_output = curl_exec ($ch);

curl_close ($ch);

// further processing ....
if ($server_output == "OK") { ... } else { ... }


No comments:

Post a Comment