Skip to content

Execute a HTTP POST Using PHP cURL

Last updated on October 12, 2014

cURL is very powerful library, which allows transfer of data across a wide variety of protocols. I often use curl in my projects. here is a very basic example to Post data using cURL in PHP.

// initiate cURL resource
$ch = curl_init();    

// where you want to post data
$url = "http://www.host.com/curl_file.php";

curl_setopt($ch, CURLOPT_URL,$url);

// tell curl you want to post something
curl_setopt($ch, CURLOPT_POST, true);  

// define what you want to post
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
        item1 => 'value',
        item2 => 'value2'
    ));

// return the output in string format
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// execute post
$output = curl_exec ($ch);

// close the connection
curl_close ($ch);

// show output
var_dump($output);

That’s it.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments