HTTP POST Using PHP CURL

Share on TwitterDigg This

what is cURL

cURL lib in php allows you to connect and communicate to many different types of servers with many different types of protocols. libcurl currently supports the http, https, ftp, gopher, telnet, dict, file, and ldap protocols. libcurl also supports HTTPS certificates, HTTP POST, HTTP PUT, FTP uploading (this can also be done with PHP’s ftp extension), HTTP form based upload, proxies, cookies, and user+password authentication.

curl_init() Initialize a cURL session

curl_init() return a cURL handle or object for use with the curl_setopt(), curl_exec(), and curl_close() functions.
for eg:

<?php
// create a new cURL resource
$ch = curl_init();
 
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);
 
// grab URL and pass it to the browser
curl_exec($ch);
 
// close cURL resource, and free up system resources
curl_close($ch);
?>

curl_setopt() — Set an option for a cURL transfer

curl_setopt ( resource $ch , int $option , mixed $value )
this function return bool.
resource is curl_init() return a cURL handle or object.
$option is in the format The CURLOPT_XXX option to set.
for eg:

//$value TRUE to do a regular HTTP POST.
 curl_setopt ($ch, CURLOPT_POST, 1);
//$value is post filed, setup post fields
 curl_setopt ($ch, CURLOPT_POSTFIELDS,"Message=$message");
//$value is TRUE to prepare for an upload
 curl_setopt ($ch,CURLOPT_UPLOAD,1);

curl_exec — Perform a cURL session or execute curl session

This function should be called after initializing a cURL session and all the options for the session are set.
Returns TRUE on success or FALSE on failure.

//execute and geting response
 $curlresponse = curl_exec($ch);

curl_close() — Close a cURL session

curl_close ( resource $ch )
Closes a cURL session and frees all resources. The cURL handle, ch, is also deleted.

<?php
// create a new cURL resource
$ch = curl_init();
 
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);
 
// grab URL and pass it to the browser
curl_exec($ch);
 
// close cURL resource, and free up system resources
curl_close($ch);
?>
</php>

How to download a webiste using php curl

<?php
function download_wesite($Url){
 
    // is cURL installed yet?
    if (!function_exists('curl_init')){
        die('Sorry cURL is not installed!');
    }
 
    // OK cool - then let's create a new cURL resource handle
    $ch = curl_init();
 
    // Now set some options (most are optional)
 
    // Set URL to download
    curl_setopt($ch, CURLOPT_URL, $Url);
 
    // Set a referer
    curl_setopt($ch, CURLOPT_REFERER, "http://www.example.org/yay.htm");
 
    // User agent
    curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
 
    // Include header in result? (0 = yes, 1 = no)
    curl_setopt($ch, CURLOPT_HEADER, 0);
 
    // Should cURL return or print out the data? (true = return, false = print)
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 
    // Timeout in seconds
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
 
    // Download the given URL, and return output
    $output = curl_exec($ch);
 
    // Close the cURL resource, and free system resources
    curl_close($ch);
 
    return $output;
}
 
echo download_website('http://www.example.org/');
?>

How to execute a POST using the PHP CURL library.

//define post values 
$message="test message ";//define post url
 $url="http://localhost/WebServices/";//encode the message
 $message = urlencode($message);//initialize the curl
 $ch = curl_init();
 
 if (!$ch){
die("Couldn't initialize a cURL handle");
}//setting options
 $ret = curl_setopt($ch, CURLOPT_URL,$url);
  curl_setopt ($ch, CURLOPT_POST, 1);
 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);       
 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
//setup post fields
 curl_setopt ($ch, CURLOPT_POSTFIELDS,"Message=$message");
 $ret = curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//If you are behind proxy then please uncomment below line and provide your proxy ip with port.
// $ret = curl_setopt($ch, CURLOPT_PROXY, "PROXY IP ADDRESS:PORT");
 
//execute and geting response
 $curlresponse = curl_exec($ch);
 
if(curl_errno($ch))
    echo 'curl error : '. curl_error($ch);
 
 if (empty($ret)) {
    // some kind of an error happened
    die(curl_error($ch));
    curl_close($ch); // close cURL handler
 } else {
    $info = curl_getinfo($ch);
    curl_close($ch); // close cURL handler
    //echo "<br>";
    echo $curlresponse;//echo "post message Sent Succesfully" ;
    }
 }

Related

About prabin t p

Connect with me on Google+
This entry was posted in php and tagged , , , . Bookmark the permalink.

14 Responses to HTTP POST Using PHP CURL

  1. I just prefer this content a lot, I actually do hope you would likely blog a lot more about this.

  2. An exceedingly great publish. This short article covers for me personally what this subject is determined by and some from the important benefits that may be resulting from being aware of it as if you. A friend once pointed out that you have a totally different mindset when you make a move for certain as opposed to when youre simply just toying with it. When it comes to this kind of topic, In my opinion you’re taking, or start to think of, a more professional in addition to thorough method of both exactly what and how you are writing, which in turn helps you to carry on and get much better as well as manual other people who have no idea something about what you have discussed right here. Thank you.

  3. A remarkable share, I recently given this onto a colleague who had previously been performing a little evaluation during this. After which he actually bought me personally breakfast since i have discovered it with regard to him.. smile. Therefore let me edit that: Thnx for the treat! However yeah Thnkx with regard to spending whenever to go over this, I find myself highly relating to this as well as really like reading through regarding this topic. If at all possible, while you develop expertise, can you thoughts upgrading your website that has a lot additional information? It is extremely great for me personally. Huge thumb upward because of this short article!

  4. I believe one of your ads triggered my internet browser to resize, you might want to put that on your blacklist.

    • jameel says:

      Ok, ma dopo che ho uno sha . sai che roba.Oltrettutto quelle panige spesso cambiano ma non contenuti per cui ci hanno pif9 falsi positivi che altro

      • Teresa says:

        I actually came here to ask you if the easyOne theme will work with Google chmroe? i loved the layout and design..please reply soon, as i need to download and implement this onto my website!!

  5. source says:

    Have you given any kind of consideration at all with converting your blog in to French? I know a several of translaters right here which might help you do it for free if you wanna contact me personally.

  6. news says:

    If you could email me with a few hints & tips about how you made this blog look this good, I would be appreciative.

  7. Meire says:

    Love the wreath. That is one of my fortviae looks for valentines decor. One of our local Michaels had a 5 foot Christmas tree covered in paper roses. It would take an army to make enough flowers to cover a tree that large.HINT: when cutting foam, use a longer knife. One of the retractable box knives with the long blades works well. Be sure that only the blade touches the foam, not the handle and just slice through the foam. The knife MUST be sharp. Much less “snow” this way.

  8. John says:

    shoeless happy to have bumped’ into you here ( you made a cemnomt on Half Broke Horses my little blog Half-Broke Bookclub i’l check in from time to time ..tom honea fairview ( asheville ) nc

  9. Thank you a lot for sharing this with all of us you really realize what you’re speaking approximately! Bookmarked. Kindly additionally discuss with my web site =). We may have a hyperlink trade contract between us

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

*