2015-12-12 68 views
0

我想检查一个页面是否使用curl给出200标题。curl获取标题

我使用下面的脚本:

public static function isUrlExist($url) 
{ 
    $curl = curl_init($url); 
    curl_setopt($curl, CURLOPT_NOBODY, TRUE); 
    curl_exec($curl); 
    $code = curl_getinfo($curl, CURLINFO_HTTP_CODE); 
    if ($code == 200) { 
     $status = true; 
    } else { 
     $status = false; 
    } 
    curl_close($curl); 
    return $status; 
} 

奇怪的是它的计算结果为true,如果我查询网站,如VIMEO:https://vimeo.com/api/oembed.json?url=https://vimeo.com/11896354

但网站如Facebook或谷歌返回false。

我错过了什么吗?

+0

你检查了状态码(和答复的其余部分)吗?您可能会遇到重定向,或者他们可能会阻止使用像curl默认的用户代理的请求。 – jcaron

回答

3

我最好的猜测是facebook/google正在重定向你,导致3xx重定向状态码。尝试在curl_exec()之前添加curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);以使curl跟随重定向。