How to download webpage content using PHP CURL

By

September 5, 2011PHP2 Comments

php curl

PHP CURL-The powerful and useful library to fetch contents from websites remotely.Using Php CURL you can able to download the webpage source of any website and store it to a php variable.Curl requests to any webpage server and downloads the content.

See The rest of PHP beginner tutorials

Processflow

  1. Initialize Curl
  2. Set Curl Options
  3. Execute Curl
  4. Close curl

Code

< ?
$ch = curl_init(); // Initialize Curl
$url="http://devlup.com"; //URL of the webpage you want to download
curl_setopt($ch, CURLOPT_URL, $url); // Set CURL options
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); //Return the handle if the curl session is set
$output = curl_exec($ch); // execute the curl
curl_close($ch); // close the curl
?>

In this way you can download the source page of the url to $output variable.There are many curl options (curl_setopt) that can be used according to your applications.See the list of curl options.

Many php programmers use file_get_contents() function to download content from websites but this has the limitation, you cannot download webpages that uses secure urls (https://).The other disadvantage of file_get_contents() is slower performance when compared to Curl.

Read our basic PHP tutorials to get started in PHP programming.If you need clarification regarding php CURL ,Post your comments below.

2 Responses to “How to download webpage content using PHP CURL”
  1. umashankar

    Then what about HTTrack ?Is it like CURL?

    • Jeyaganesh

      CURL is different from HTTrack. PHP CURL is the standard library for PHP to extract web page contents.

Leave a Reply

*