Download a URL's Content Using PHP CURL
Downloading content at a specific URL is common practice on the internet, especially due to increased usage of web services and APIs offered by Amazon, Alexa, Digg, etc. PHP's CURL library, which often comes with default shared hosting configurations, allows web developers to complete this task.
The Code
/* gets the data from a URL */
function get_data($url)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
The Usage
$returned_content = get_data('http://davidwalsh.name');
Alternatively, you can use the file_get_contents function remotely, but many hosts don't allow this.
- Login or register to post comments
- 1342 reads
- Printer-friendly version
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)










Comments
gauravpadia replied on Fri, 2008/11/14 - 3:27pm
PatrickMc replied on Fri, 2008/12/19 - 4:28pm
cat "http://abc.def.ghi/.../page.html"
(abc, def, ghi, page are all examples.)
To save the contents of the URL to a file,
cat "http://abc.def.ghi/.../page.html" > saved_copy.html
Patrick
elandirayan replied on Tue, 2009/03/10 - 11:14pm
Hai,
Fatal error: Call to undefined function curl_init() in D:\Program Files\xampp\htdocs\ctest.php on line 8
i got the above mentioned error. Let me know why such error.