0

如果我得到页面的标题,我可以判断下载链接是活动的还是失效的。

例如:“免费在线存储”是死链接的标题,“ [文件名] ”是活动链接的标题(mediafire)。但是我的页面响应时间太长,那么有没有其他方法可以检查下载链接是活动的还是失效的?

这就是我所做的:

<?php

function getTitle($Url){
$str = file_get_contents($Url);
if(strlen($str)>0){
    preg_match("/\<title\>(.*)\<\/title\>/",$str,$title);
    return $title[1];
}
}

?>
4

3 回答 3

2

不执行 GET 请求,下载整个页面/文件,而是执行 HEAD 请求,仅获取 HTTP 标头,并检查状态是否为 200,并且内容类型不是 text/html

于 2012-12-21T12:22:38.077 回答
1

像这样的东西...

function url_validate($link)
{
    #[url]http://www.example.com/determining-if-a-url-exists-with-curl/[/url]
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $link);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_MAXREDIRS, 10); //follow up to 10 redirections - avoids loops
    $data = curl_exec($ch);
    curl_close($ch);
    preg_match_all("/HTTP\/1\.[1|0]\s(\d{3})/",$data,$matches);

    $code = end($matches[1]);

    if(!$data) 
    {
        return(false);
    } 
    else 
    {
        if($code==200) 
        {
            return(true);
        } 
        elseif($code==404) 
        {
            return(false);
        }
    }
}

您可以安全地使用任何 cURL 库函数。这是合法的,因此不会被视为黑客攻击。唯一的要求是您的网络托管公司安装了 cURL 扩展,这很有可能。

于 2012-12-21T12:26:37.317 回答
0

cURL应该做的工作。如果需要,您也可以检查返回的标题和文本内容。

于 2012-12-21T12:23:46.070 回答