Sunday, July 17, 2011

PHP Function to Check URL

The following PHP function is used to check if the URL exists. This php function will monitor the http requests and returns the header information. This function is very useful when you are validating the given URL

function checkUrl($url) {
$root = _getRoot($url);
$ctx = stream_context_create(array(
'http' => array(
'max_redirects' => 5, // allows 4 redirects :/
'timeout' => 5,
)
));
$fp = @fopen($url, 'r', false, $ctx);
$ret['success'] = false;
if (! $fp) {
return $ret;
}
$ret['locations'] = array();
$ret['metaData'] = stream_get_meta_data($fp);
fclose($fp);
// analyze HTTP headers
foreach ($ret['metaData']['wrapper_data'] as $line) {
if (preg_match('@^Location: (.*)$@i', $line, $m)) {
if ($m[1][0] === '/') {
// root-relative URI
$m[1] = $root . $m[1];
} elseif (strpos($m[1], '://') >= 4) {
// full URL
$root = _getRoot($m[1]);
}
$ret['locations'][] = $m[1];
}
if (preg_match('@^HTTP/1\.[01] 200@i', $line, $m)) {
$ret['success'] = true;
}
}
return $ret;
}

function _getRoot($url) {
list($proto, $url) = explode('://', $url, 2);
list($host) = explode('/', $url, 2);
return $proto . '://' . $host;
}



var_export(checkUrl('http://microsoft.com/ie'));

// outputs

array (
'success' => true,
'locations' =>
array (
0 => 'http://www.microsoft.com/ie',
1 => 'http://www.microsoft.com/ie/',
2 => 'http://www.microsoft.com/windows/internet-explorer/default.aspx',
),
'metaData' =>
array (
'wrapper_data' =>
array (
... abunch of HTTP headers
),
'wrapper_type' => 'http',
'stream_type' => 'tcp_socket/ssl',
'mode' => 'r+',
'unread_bytes' => 3478,
'seekable' => false,
'uri' => 'http://microsoft.com/ie',
'timed_out' => false,
'blocked' => true,
'eof' => false,
),
)



Check these out

http://hungred.com/how-to/php-check-remote-email-url-image-link-exist/

http://w-shadow.com/blog/2007/08/02/how-to-check-if-page-exists-with-curl/

No comments:

Post a Comment