Simple cURL example

A simple example on how to use the cURL module to download a website or any other file.

You can find more cURL options on the related PHP manual page.

function curl_download($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;
}
Snippet information

Author:
Jonas John

License:
Public Domain

Language:
PHP

Created:
04/27/2006

Updated:
03/11/2011

Tags:
, ,



print curl_download('http://www.example.org/');

Sorry folks, comments have been deactivated for now due to the large amount of spam.

Please try to post your questions or problems on a related programming board, a suitable mailing list, a programming chat-room,
or use a QA website like stackoverflow because I'm usually too busy to answer any mails related
to my code snippets. Therefore please just mail me if you found a serious bug... Thank you!


Older comments:

ds June 22, 2011 at 08:11
da
moorie May 24, 2011 at 14:02
its working... no bug at all. Thanks a lot
Raju May 11, 2011 at 11:31
Hello boss,
I m totally confuse if came in chandigarh plz met with me
ok
Vabhav May 02, 2011 at 12:12
its really good And working n help me alot...
Jonas April 15, 2011 at 23:15
Thanks Rahul ;-)
Rahul April 12, 2011 at 13:24
Thank you so much, if you ever come down to bangalore, we can for a lunch :P !!!!!!!!!!!!!!!11
Bhushan March 30, 2011 at 04:52
very nice post its clear my all doughts.
Thanks
g March 27, 2011 at 10:42
dfg
dfg March 18, 2011 at 10:53
dfgfg
Kunal Mazumder September 28, 2010 at 23:51
Very handy and ready to use. Thanks
sandeep July 15, 2010 at 04:59
thanks alot, setting up all _setopt is compulsory ??
Natalie April 07, 2010 at 19:09
Thank you.
Sachin February 05, 2010 at 14:15
Good Article
Can i login with curl or post form values using curl.
Sam January 08, 2010 at 17:22
Thanks for example.
Niranjan: you can use eval(), but it hase some security issues...
john December 10, 2009 at 16:25
Very helpful! Thank you!
Craig R Morton August 17, 2009 at 12:45
Many thanks, this worked well for me to check if I had cURL setup correctly on my machine. Perfect for a PHP beginner. :-)
Niranjan Ramakrishnan July 25, 2009 at 11:48
I'm trying to use curl to replace the includes in my code. I've run into a problem. Suppose I am trying to include foreign.php into my fiile native.php. Assume that foreign.php contains a variable $xyz. The include works fine, but I am unable to use $xyz inside native.php. In our system, register_globals is turned off. What is the way around this?
Chanon February 11, 2009 at 13:03
Thanks mate

This code and comment is really clear and useful for the beginner like me.
Great job!