如何编写一个PHP脚本来获取给定用户在Twitter上的关注者数量?

如何编写一个PHP脚本来获取给定用户在Twitter上的关注者数量?,php,mysql,json,twitter,twitter-oauth,Php,Mysql,Json,Twitter,Twitter Oauth,John Mayer说,我所要做的就是获取给定用户的Twitter关注者数量。我想将这个值存储在变量$testCount中,就这样。我没有与任何其他用户一起编写应用程序或任何东西(所有文档似乎都围绕着其他用户将这样做的假设) 我之所以存储该值,是因为在我的PHP脚本中,我只想将该值存储在mySQL表中。存储值不是问题,而是从Twitter的API中检索。最终的目标是每天运行这个脚本,并让它获得John Mayer追随者的最新计数 我查看了其他堆栈溢出的答案,并尝试了J7mbo的“TwitterA

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'];

回声根本不打印任何东西。我已经为此工作了好几天,似乎不知道如何使用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=getWitterFollers('dadalife')


祝所有在推特新的荒谬API中挣扎的人好运,
echo'.print\r($testCount,1)。''打印任何内容?它打印以下内容:数组([errors]=>Array([0]=>Array([message]=>Bad Authentication data[code]=>215)),只需读取错误消息即可。是的,它的错误是215(Bad Authentication data)。我在谷歌上搜索了这个错误,也没有解决方案,但似乎其他人也有这个问题。你应该将你的URL修改为
https://api.twitter.com/1.1/users/show.json
因为此URL将在签名中使用,而带有参数的URL在Twitter授权上不起作用。这样试试。