我需要定期循环遍历PHP数据库中的链接,以检查链接是否指向有效页面。如果链接已过期或无效,我不想输出它。如何检查href值是否有效地导致有效页面?
感谢任何*指针。
答案 0 :(得分:1)
看看卷曲。它允许您在php http://www.php.net/manual/en/function.curl-exec.php中提取网站然后只需检查响应中的状态代码或类似标题标记的内容。
答案 1 :(得分:1)
您还可以每次使用多个CUrl请求来更快地检查所有列表。 Check here
答案 2 :(得分:0)
我自己就是一个菜鸟,但我建议使用cURL。谷歌快速使用搜索显示以下代码(我还没有测试过):
<?php
$statusCode = validate($_REQUEST['url']);
if ($statusCode==’200′)
echo ‘Voila! URL ‘.$_REQUEST['url'].
’ exists, returned code is :’.$statusCode;
else
echo ‘Opps! URL ‘.$_REQUEST['url'].
’ does NOT exist, returned code is :’.$statusCode;
function validateurl($url)
{
// Initialize the handle
$ch = curl_init();
// Set the URL to be executed
curl_setopt($ch, CURLOPT_URL, $url);
// Set the curl option to include the header in the output
curl_setopt($ch, CURLOPT_HEADER, true);
// Set the curl option NOT to output the body content
curl_setopt($ch, CURLOPT_NOBODY, true);
/* Set to TRUE to return the transfer
as a string of the return value of curl_exec(),
instead of outputting it out directly */
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute it
$data = curl_exec($ch);
// Finally close the handle
curl_close($ch);
/* In this case, we’re interested in
only the HTTP status code returned, therefore we
use preg_match to extract it, so in the second element
of the returned array is the status code */
preg_match(“/HTTP\/1\.[1|0]\s(\d{3})/”,$data,$matches);
return $matches[1];
}
?>
来源:http://www.ajaxapp.com/2009/03/23/to-validate-if-an-url-exists-use-php-curl/