2013-07-12 52 views
0

我需要一種方法來檢查tweet是否存在。我有鏈接到鳴叫,如https://twitter.com/darknille/status/355651101657280512。我最好要一個快速的方法來檢查(無需檢索頁面,只是HEAD請求體),所以我想是這樣的檢查微博狀態是否存在?

function if_curl_exists($url) 
{ 

    $resURL = curl_init(); 
    curl_setopt($resURL, CURLOPT_URL, $url); 
    curl_setopt($resURL, CURLOPT_BINARYTRANSFER, 1); 
    curl_setopt($resURL, CURLOPT_HEADERFUNCTION, 'curlHeaderCallback'); 
    curl_setopt($resURL, CURLOPT_FAILONERROR, 1); 
    $x = curl_exec ($resURL); 
    //var_dump($x); 
    echo $intReturnCode = curl_getinfo($resURL, CURLINFO_HTTP_CODE); 
    curl_close ($resURL); 
    if ($intReturnCode != 200 && $intReturnCode != 302 && $intReturnCode != 304) { 
     return false; 
    } 
    else return true; 

} 

或類似這樣的

function if_curl_exists_1($url) 
{ 
    $curl = curl_init($url); 
    curl_setopt($curl, CURLOPT_NOBODY, true);//head request 
    $result = curl_exec($curl); 

    $ret = false; 

    if ($result !== false) { 
     //if request was ok, check response code 
     echo $statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); 

     if ($statusCode == 200) { 
      $ret = true; 
     } 
    } 

    curl_close($curl); 
    return $ret; 
} 

但是這兩個返回null與curl_exec() ,沒有什麼可以檢查http狀態碼。

另一種方法是使用Twitter的API,像GET statuses/show/:idhttps://dev.twitter.com/docs/api/1.1/get/statuses/show/%3Aid但如果推不存在,這裏https://dev.twitter.com/discussions/8802

我需要諮詢最新最快的方法來檢查,我說沒有什麼特別的返回值在php中做。

+0

如果沒有推文匹配'id',twitter API調用會返回什麼內容? – dm03514

+0

Docs對此沒有提及,因爲您可以在上面的鏈接中閱讀。 –

回答

0

你可能必須設置返回搬運標誌

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 

如果代碼返回爲30X狀態你可能要增加以下位置標誌以及

curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); 
+0

'$ url ='https://twitter.com/darknille/status/355651101657280512'////假即使tweet存在 \t // $ url ='http://google.com'; //作品200 \t // $ url ='http://php.net'; //作品200 \t if(if_curl_postoji1($ url)) \t \t echo「yes」; \t else echo「no」;' –

+0

所以它適用於所有網站,如谷歌,php.net但不適用於Twitter,就像他們的服務器設置爲不響應HEAD請求或一些額外的http params。我猜測https://被使用的事實不會影響事物。 –

+0

只是說我添加了2個捲曲選項。 –

0

您可以使用@get_header 。它將返回一個數組,其中第一個項目具有響應代碼:

$response = @get_headers($url); 
print_r($response[0]); 
if($response[0]=='HTTP/1.0 404 Not Found'){ 
    echo 'Not Found'; 
}else{ 
    echo 'Found'; 
}