目前我正在使用以下功能
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;
}
Run Code Online (Sandbox Code Playgroud)
它适用于简单的URL,但不适用于重定向到另一个域的URL
你需要设置FOLLOWLOCATION:
curl_setopt($handle, CURLOPT_FOLLOWLOCATION, true);
Run Code Online (Sandbox Code Playgroud)
但是,在此发布GET请求没有意义.简单的HEAD更轻,因为只传输标题.为此,请设置NOBODY为true:
curl_setopt($handle, CURLOPT_NOBODY, true);
Run Code Online (Sandbox Code Playgroud)