William Jiang

JavaScript,PHP,Node,Perl,LAMP Web Developer – http://williamjxj.com; https://github.com/williamjxj?tab=repositories

PHP cURL Library

cURL (client URL library) is a library which allows you to connect and communicate to many different types of servers with many different types of protocols. Using cURL you can:

  • Implement payment gateways’ payment notification scripts.
  • Download and upload files from remote servers.
  • Login to other websites and access members only sections.

PHP cURL library is definitely the odd man out. Unlike other PHP libraries where a whole plethora of functions is made available, PHP cURL wraps up a major parts of its functionality in just four functions.

A typical PHP cURL usage follows the following sequence of steps.

  1. curl_init – Initializes the session and returns a cURL handle which can be passed to other cURL functions.
  2. curl_opt – This is the main work horse of cURL library. This function is called multiple times and specifies what we want the cURL library to do.
  3. curl_exec – Executes a cURL session.
  4. curl_close – Closes the current cURL session.

Below are some examples which should make the working of cURL more clearer: use cURL to download Google’s RSS feed.

<?php
/**
* Initialize the cURL session
*/
$ch = curl_init();
/**
* Set the URL of the page or file to download.
*/
curl_setopt($ch, CURLOPT_URL,
‘http://news.google.com/news?hl=en&topic=t&output=rss’);
/**
* Ask cURL to return the contents in a variable
* instead of simply echoing them to the browser.
*/
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
/**
* Execute the cURL session
*/
$contents = curl_exec ($ch);
/**
* Close cURL session
*/
curl_close ($ch);
?>

As you can see, curl_setopt is the pivot around which the main cURL functionality revolves. cURL functioning is controlled by way of passing predefined options and values to this function.

The above code uses two such options.

  • CURLOPT_URL: Use it to specify the URL which you want to process. This could be the URL of the file you want to download or it could be the URL of the script to which you want to post some data.
  • CURLOPT_RETURNTRANSFER: Setting this option to 1 will cause the curl_exec function to return the contents instead of echoing them to the browser.

The following is another code sample for download whole webpage from a website.

  1. function DownloadUrl(){
  2. $Url = $this->url;
  3. if (!function_exists(‘curl_init’)){
  4. die(‘CURL is not installed!’);
  5. }
  6. $ch = curl_init();
  7. curl_setopt($ch, CURLOPT_URL, $Url);
  8. curl_setopt($ch, CURLOPT_REFERER, “http://www.imdb.com/title/tt0120338/&#8221;);
  9. curl_setopt($ch, CURLOPT_USERAGENT, “MozillaXYZ/1.0”);
  10. curl_setopt($ch, CURLOPT_HEADER, 0);
  11. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  12. curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  13. $output = curl_exec($ch);
  14. curl_close($ch);
  15. return $output;
  16. }
Advertisements

4 responses to “PHP cURL Library

  1. Tony Su 11/27/2010 at 5:47 pm

    Yes, I uses curl in PHP a lot in my last job, it’s so convenient to wget a web page or forward a HTTP/HTTPS request. Love it!

  2. 關鍵字行銷 11/28/2010 at 9:29 pm

    Undoubtedly, one of the best article l have come across on this precious topic. I quite agree with your conclusions and will eagerly look forward to your coming updates.

  3. ip camera 12/15/2010 at 6:36 pm

    Good!!! Bookmarked this page that has this amazing content. Will come back to see if there are any updates. You, the author, are a master. Thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: