2011-01-14 49 views
1

當我執行下面的代碼時,需要10-12秒才能響應。爲什麼Twitter的API調用這麼慢?

問題與Twitter或與我們的服務器?

我真的需要知道,因爲這是在我們的網站上顯示推文的代碼的一部分,12秒的加載時間是不可接受的!

function get_latest_tweets($username) 
    { 
    print "<font color=red>**". time()."**</font><br>"; 
    $path = 'http://api.twitter.com/1/statuses/user_timeline/' . $username.'.json?include_rts=true&count=2'; 
    $jason = file_get_contents($path); 
    print "<font color=red>**". time()."**</font><br>"; 
    } 

感謝

+0

我覺得很難說爲什麼一個特定的web服務很慢。正如我們所談論的是一個web服務,所有讓網絡慢的問題也可以應用到twitter調用中。 – 2011-01-14 11:56:04

+0

我不知道爲什麼需要這麼長時間。但同時你可以使用本地緩存,直到它被修復。 – Simon 2011-01-14 11:56:59

回答

1

當你把網址到瀏覽器(http://api.twitter.com/1/statuses/user_timeline/ 用戶名以.json?include_rts =真&數= 2 )頁面顯示需要多長時間?如果速度很快,那麼您需要在服務器上開始搜索。

1

使用curl而不是file_get_contents()來請求,以便響應將被壓縮。這是我使用的捲曲功能。

function curl_file_get_contents($url) 
{ 
    $curl = curl_init(); 

    curl_setopt($curl,CURLOPT_URL,$url); //The URL to fetch. This can also be set when initializing a session with curl_init(). 
    curl_setopt($curl,CURLOPT_RETURNTRANSFER,TRUE); //TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly. 
    curl_setopt($curl,CURLOPT_ENCODING , "gzip"); 

    curl_setopt($curl, CURLOPT_FAILONERROR, TRUE); //To fail silently if the HTTP code returned is greater than or equal to 400. 
    curl_setopt($curl,CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE); 

    $contents = curl_exec($curl); 
    curl_close($curl); 

    return $contents; 
}