1
download url content with curl
CGuild1.com > Tips & Tricks > download url content with curl

Download URL content with PHP and cUrl

This is the basic function for downloading a URL's content. I use it as a starting point for just about every API result script I work on.

/* 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;
}


Heres how to use it...

$returned_content = get_data('http://cguild1.com');

Original source:http://davidwalsh.name/curl-download
©2024 CGuild1.com