Skip to content

Instantly share code, notes, and snippets.

@denchev
Last active August 29, 2015 14:07
Show Gist options
  • Save denchev/cb3f3d0af86657d7afef to your computer and use it in GitHub Desktop.
Save denchev/cb3f3d0af86657d7afef to your computer and use it in GitHub Desktop.
Get remote URL content
<?php
function request($url, $cache = false) {
if($cache === true) {
$name = md5($url);
if(file_exists('/tmp/' . $name)) {
$response = file_get_contents('/tmp/'.$name);
return $response;
}
}
$ch = curl_init();
$agent= 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_VERBOSE, true);
$response = curl_exec($ch);
if($cache === true) {
$name = md5($url);
file_put_contents('/tmp/' . $name, $response);
}
return $response;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment