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; }
Author:
Jonas John
License:
Public Domain
Language:
PHP
Created:
04/27/2006
Updated:
03/11/2011
Tags:
http, network, example
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:
I m totally confuse if came in chandigarh plz met with me
ok
Thanks
Can i login with curl or post form values using curl.
Niranjan: you can use eval(), but it hase some security issues...
This code and comment is really clear and useful for the beginner like me.
Great job!