Simple PHP Curl Example: Download an URL

Posted by Joys of Programming on in Curl, PHP

To work with websites, both for posting and getting data, CURL is the best option. Curl library is available for almost all languages like C/C++, Java, PHP. To start working with curl, you must install php-curl

Once installed, you can take a look at this simple example.


<?php

function downloadURL($URL) {
 if(!function_exists('curl_init')) {
 die ("Curl PHP package not installed\n");
 }

 /*Initializing CURL*/
 $curlHandle = curl_init();

 /*The URL to be downloaded is set*/
 curl_setopt($curlHandle, CURLOPT_URL, $URL);

 /*Now execute the CURL, download the URL specified*/
 $response = curl_exec($curlHandle);
 return $response;
}

echo downloadURL("http://www.mozilla.org/");
?>

Specify any parameter (URL of a webpage) for the function downloadURL. See the output of the script on the browser or the console. You can see the HTML source code of the webpage

Tags: , , ,

Comments:

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>

Copyright © 2009-2012 Joys of Programming All rights reserved.
Desk Mess Mirrored v1.8.1 theme from BuyNowShop.com.