I wrote the following to see if a submitted URL has a valid http response code and also if it responds quickly.
Use the code like this:
<?php
$is_ok = http_response($url); ?>
The second argument is optional, and it allows you to check for a specific response code
<?php
http_response($url,'400'); ?>
The third allows you to specify how long you are willing to wait for a response.
<?php
http_response($url,'200',3); ?>
<?php
function http_response($url, $status = null, $wait = 3)
{
$time = microtime(true);
$expire = $time + $wait;
$pid = pcntl_fork();
if ($pid == -1) {
die('could not fork');
} else if ($pid) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_NOBODY, TRUE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$head = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if(!$head)
{
return FALSE;
}
if($status === null)
{
if($httpCode < 400)
{
return TRUE;
}
else
{
return FALSE;
}
}
elseif($status == $httpCode)
{
return TRUE;
}
return FALSE;
pcntl_wait($status); } else {
while(microtime(true) < $expire)
{
sleep(0.5);
}
return FALSE;
}
}
?>
Hope this example helps. It is not 100% tested, so any feedback [sent directly to me by email] is appreciated.