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:
1 2 3 4 5 6 7 8 9 10 11 12 |
/* 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; } |
/* 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:
1 |
$returned_content = get_data('http://davidwalsh.name'); |
$returned_content = get_data('http://davidwalsh.name');
Alternatively, you can use the file_get_contents function remotely, but many hosts don’t allow this.
Info from: http://davidwalsh.name
PHP File Upload Tutorial Linux Delete All Files In Directory
![[Google]]( http://www.network-content.com/wp-content/plugins/easy-adsense-lite/google-light.gif)