1

目前我正在使用以下功能

function urlExist($url)
{
                $handle   = curl_init($url);
                if (false === $handle)
                {
                        return false;
                }
                curl_setopt($handle, CURLOPT_HEADER, false);
                curl_setopt($handle, CURLOPT_FAILONERROR, true);  // this works
                curl_setopt($handle, CURLOPT_HTTPHEADER, Array("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15") ); // request as if Firefox
                curl_setopt($handle, CURLOPT_NOBODY, true);
                curl_setopt($handle, CURLOPT_RETURNTRANSFER, false);
                $connectable = curl_exec($handle);
                ##print $connectable;
                curl_close($handle);
                return $connectable;
}

它适用于简单的 url,但不适用于重定向到另一个域的 url

4

2 回答 2

6

您需要设置FOLLOWLOCATION

curl_setopt($handle, CURLOPT_FOLLOWLOCATION, true);

但是,在这里发出 GET 请求是没有意义的。一个简单的 HEAD 更轻,因为只传输标题。为此,请设置NOBODY为 true:

curl_setopt($handle, CURLOPT_NOBODY, true);
于 2012-04-11T09:52:37.047 回答
0

我正在使用相同的功能,但我没有遇到域 trieuvieclam.com 重定向到新域的问题。我正在使用 CHROME 浏览器

function url_exists($url) {
    $handle   = curl_init($url);
    if (false === $handle)
    {
            return false;
    }

    curl_setopt($handle, CURLOPT_HEADER, false);
    curl_setopt($handle, CURLOPT_FAILONERROR, true);  // this works
    curl_setopt($handle, CURLOPT_HTTPHEADER, Array("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15") ); // request as if Firefox
    curl_setopt($handle, CURLOPT_NOBODY, true);
    curl_setopt($handle, CURLOPT_RETURNTRANSFER, false);
    curl_setopt($handle, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 3);
    $connectable = curl_exec($handle);
    ##print $connectable;
    curl_close($handle);
    if($connectable){
        return true;
    }
    return false;
}

我尝试将这一行 : 更改curl_setopt($handle, CURLOPT_FOLLOWLOCATION, true);false,但结果仍然相同(现有域)。

if (false === $handle)
        {
                return false;
        }

这个条件永远不会满足任何字符串,甚至不是 url,也许它只在服务器不支持 curl 时才匹配。

于 2016-10-11T09:12:29.240 回答