Skip to content

Instantly share code, notes, and snippets.

@angelinetran
Created July 31, 2017 03:18
Show Gist options
  • Save angelinetran/0e554340797846e9aaacea1f26978de1 to your computer and use it in GitHub Desktop.
Save angelinetran/0e554340797846e9aaacea1f26978de1 to your computer and use it in GitHub Desktop.
/**
* Read file using cURL
*
* @param $url
* @param $header_params array
* @return $data
*
*/
function fetch($url, $header_params = "") {
$url = preg_replace('/\s+/', '+', $url);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
if($header_params !== "") {
curl_setopt($ch, CURLOPT_HTTPHEADER, $header_params);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Set the maximum curl connect timeout to 6 seconds and then the complete request to 10 seconds; this bypasses the $connect_timeout and $timeout variables
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, 6000);
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 10000);
$data = curl_exec($ch);
if(curl_exec($ch) === false) {
if ( $this->debug ) { $this->return_data .= "<p>curl: " . curl_error($ch) . "</p>\n"; }
return FALSE;
}
curl_close($ch);
return $data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment