[英]How can I write a PHP script that will grab the follower count in Twitter for a given user?
John Mayer說,我要做的就是獲取給定用戶的Twitter關注者計數。 我想將此值存儲在變量$ testCount中,僅此而已。 我不是在與任何其他用戶一起編寫應用程序或任何東西(所有文檔似乎都圍繞着其他用戶會這樣做的假設)
之所以存儲該值,是因為在我的PHP腳本中,我要做的就是將該值存儲在mySQL表中。 存儲價值不是問題,而是從Twitter的API中獲取價值。 最終目標是每天運行此腳本,並讓其獲得John Mayer的關注者的更新計數。
我查看了其他堆棧溢出答案,並嘗試了J7mbo的“ TwitterAPIExchange”方法。 到目前為止,我有這個:
require_once('TwitterAPIExchange.php'); // TwitterAPIExchange.php is in the same folder as the //script I am modifying
$settings = array(
'oauth_access_token' => " my token",
'oauth_access_token_secret' => "my secret",
'consumer_key' => "my consumer key",
'consumer_secret' => "my consumer secret"
);
$url = 'https://api.twitter.com/1.1/users/show.json?screen_name=JohnMayer';
$requestMethod = 'GET';
$getfield = '?screen_name=JohnMayer';
$twitter = new TwitterAPIExchange($settings);
$follow_count=$twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
$testCount = json_decode($follow_count, true);
echo $testCount[0]['user']['followers_count'];
Echo根本不打印任何內容。 我已經為此工作了好幾天,似乎無法弄清楚如何使用php來掌握這一統計數據。 請有人幫助我調整或更改當前腳本,以實現這個看似簡單的目標。
對於任何人,這可能會有所幫助,我想出了辦法。 這是我獲得twitter關注者的函數,它是對Codeforest的一個修改,但我取出了一堆涉及cache.php的東西,以及一堆導致其出錯的其他東西。
function getTwitterFollowers($screenName = 'codeforest')
{
require_once('TwitterAPIExchange.php');
// this variables can be obtained in http://dev.twitter.com/apps
// to create the app, follow former tutorial on http://www.codeforest.net/get-twitter-follower-count
$settings = array(
'oauth_access_token' => "youraccesstoken",
'oauth_access_token_secret' => "youraccesstokensecret",
'consumer_key' => "yourconsumerkey",
'consumer_secret' => "yourconsumersecret"
);
// forming data for request
$apiUrl = "https://api.twitter.com/1.1/users/show.json";
$requestMethod = 'GET';
$getField = '?screen_name=' . $screenName;
$twitter = new TwitterAPIExchange($settings);
$response = $twitter->setGetfield($getField)
->buildOauth($apiUrl, $requestMethod)
->performRequest();
$followers = json_decode($response);
$numberOfFollowers = $followers->followers_count;
return $numberOfFollowers;
}
現在這段代碼起作用了,我可以將關注者計數存儲在變量中,如下所示:$ twitterfollow = getTwitterFollowers('dadalife');
祝其他人在Twitter的新荒謬API上苦苦掙扎
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.