Reputation: 123
I used @file_get_contents and get_headers, both are slow even if the URL is below 1ko, tried to use cURL but it isn't supported by the server. Is there any fast fucntion to use instead ?
Upvotes: 0
Views: 2018
Reputation: 11
Frank Koehl did a neat little function to do just that and return the url’s http status.
/**
* @author Frank Koehl
* @src http://frankkoehl.com/2009/09/http-status-code-curl-php/
*/
function get_status($url)
{
// must set $url first. Duh...
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
// do your curl thing here
$data = curl_exec($ch);
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $http_status;
}
Upvotes: 1
Reputation: 33823
using the method suggested by @arkascha you could do something like this:
$url='http://stackoverflow.com';
$options=array(
'http'=>array(
'method' => 'HEAD',
'User-Agent' => $_SERVER['HTTP_USER_AGENT']
)
);
stream_context_get_default( $options );
$headers=get_headers( $url, 1 );
echo $headers[0];
It seems fairly quick and you could further parse the response to find if the status is 200 or otherwise.
Upvotes: 1