簡體   English   中英

獲取Twitter用戶數

[英]Get Twitter user count

我已經在網站中繼承了這部分(PHP)代碼,該網站獲取了該帳戶的Twitter關注者,但它在錯誤日志中返回以下內容:

PHP Warning:  file_get_contents(http://twitter.com/users/show/twitterusername): failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found\r

其中“ twitterusername”是我的Twitter用戶名。 如果我在瀏覽器中轉到該URL,則確實會返回未找到的頁面。 我懷疑Twitter在某個階段更改了某些內容(不知道何時),這導致此操作失敗,並且此腳本需要更新。

有人可以幫助我解決需要更改哪些內容才能返回Twitter計數嗎?

提前致謝。 代碼如下:

$cache = get_transient('twitterfollowerscount' . $username);

if ($cache) {
    $count = get_option('twitterfollowerscount' . $username);
    if($count ==0){
        $url = 'http://twitter.com/users/show/' . urlencode($username);
        $xml=file_get_contents($url);
        if (preg_match('/followers_count>(.*)</',$xml,$match)!=0) {
        $count = $match[1];
        }
        set_transient('twitterfollowerscount' . $username, 'true',  60 * 30);
        update_option('twitterfollowerscount' . $username, $count);
    }
} else {
    $url = 'http://twitter.com/users/show/' . urlencode($username);
    $xml=file_get_contents($url);
    if (preg_match('/followers_count>(.*)</',$xml,$match)!=0) {
    $count = $match[1];
    }
    set_transient('twitterfollowerscount' . $username, 'true',  60 * 30);
    update_option('twitterfollowerscount' . $username, $count);
}
return $count;
}

使用Twitter API和使用followers/list請求來獲取followers/list要容易得多。

https://dev.twitter.com/rest/reference/get/followers/list

如果Twitter像現在一樣更改URL結構中的內容,那就更好了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM