2010-07-01 53 views
1

我試圖顯示一些使用PHP給定用戶名的Twitter追隨者。我的代碼看起來像這樣:Bulletproof Twitter追隨者與PHP計數?

function tweet_count() { 

    $name = get_option('ws_twit'); 
    $twit = file_get_contents('http://twitter.com/users/show/'.$name.'.xml'); 
    $begin = '<followers_count>'; $end = '</followers_count>'; 
    $page = $twit; 
    $parts = explode($begin,$page); 
    $page = $parts[1]; 
    $parts = explode($end,$page); 
    $tcount = $parts[0]; 
    if($tcount == '') { $tcount = '0'; } 
    echo $tcount; 
} 

它通常工作...除非它不。在大多數情況下,它拋出一個錯誤:

failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in [filename] on line 8

8號線是:$twit = file_get_contents('http://twitter.com/users/show/'.$name.'.xml');

我試着硬編碼的用戶名,但效果是一樣的。我想知道這段代碼是否正常,因爲它有時可以工作。首先,我認爲這可能是Twitter的錯誤,但它經常發生這種情況。

感謝您的幫助!

+0

你怎麼樣使用XML解析器?代碼將更加可讀。 – 2010-07-01 15:25:59

+0

有趣的是,我在另一個站點上使用* exact *相同的代碼,而另一個域則一直運行。任何人都知道爲什麼? – Justine 2010-07-01 16:22:13

回答

2

最有可能是因爲tweet的能力下降這麼多!

但是,您可能還想嘗試以下操作。

$name = get_option('ws_twit'); 
$twit = file_get_contents('http://twitter.com/users/show/'.$name.'.xml'); 
preg_match_all("|<followers_count>(.*)</followers_count>|U",$twit,$followers); 
$tcount = $followers[1]; 
echo (!empty($tcount)) ? $tcount : 0; 

UPDATE 我只是想刷新網址在瀏覽器中幾次.. http://twitter.com/users/show/TWITTERNAME.xml和一些工作,但我的確得到了錯誤的請求錯誤,這是DEFF嘰嘰喳喳。

+0

同樣的結果,第二行中的「HTTP請求失敗!HTTP/1.1 400錯誤請求」。 – Justine 2010-07-01 16:21:30

0

我同意蜥蜴在推特是關閉了很多,這可能會導致你的不一致的行爲。我使用了與你的Twitter類似的調用結構來與Twitter進行通信,但是使用了這個URL:http://twitter.com/statuses/user_timeline.xml?user_id=#######而不是/users/show/name.xml,我不確定這是否有幫助,但值得一試。也許這會更可靠,即使它也會下降。

(同樣,微博今天已經尤其糟糕:http://dl.dropbox.com/u/2320369/twitter_problems.png

+0

我在想Twitter很多,導致了這個問題,但是我經常喜歡這種方式。這就是爲什麼我試圖找到一個防彈解決方案;) – Justine 2010-07-01 15:52:04

+0

此外,我的'/用戶/ show/name.xml'在這個特定的時刻正常工作,但'/ statuses/user_timeline.xml'返回「錯誤的請求,未經授權的線blahblah「。 – Justine 2010-07-01 15:57:32