简体   繁体   English

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

[英]How can I write a PHP script that will grab the follower count in Twitter for a given user?

All I want to do is get the Twitter follower count for a given user, say John Mayer. John Mayer说,我要做的就是获取给定用户的Twitter关注者计数。 I want to store this value in a variable $testCount and that's it. 我想将此值存储在变量$ testCount中,仅此而已。 I'm not writing an application or anything with any other users (all the documentation seems to revolve around the assumption that other users will be doing this) 我不是在与任何其他用户一起编写应用程序或任何东西(所有文档似乎都围绕着其他用户会这样做的假设)

The reason I'm storing the value is because in my PHP script, all I want to do is store this value in a mySQL table. 之所以存储该值,是因为在我的PHP脚本中,我要做的就是将该值存储在mySQL表中。 Storing the value isn't the problem, it's retrieving it from Twitter's API. 存储价值不是问题,而是从Twitter的API中获取价值。 The eventual goal is to run this script every day and have it get an updated count of John Mayer's followers. 最终目标是每天运行此脚本,并让其获得John Mayer的关注者的更新计数。

I've looked at other stack overflow answers and tried J7mbo's 'TwitterAPIExchange' method. 我查看了其他堆栈溢出答案,并尝试了J7mbo的“ TwitterAPIExchange”方法。 So far I have this: 到目前为止,我有这个:

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

the Echo simply doesn't print anything. Echo根本不打印任何内容。 I've been working at this for days and can't seem to figure out how to grab this one statistic using php. 我已经为此工作了好几天,似乎无法弄清楚如何使用php来掌握这一统计数据。 Please someone help me adjust or alter my current script to achieve this seemingly simple goal. 请有人帮助我调整或更改当前脚本,以实现这个看似简单的目标。

For anyone this might help, I figured it out. 对于任何人,这可能会有所帮助,我想出了办法。 Here is my function that gets twitter followers, it is a modification on one from Codeforest but I took out a bunch of stuff involving something called cache.php and a bunch of other stuff that caused it to have errors. 这是我获得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;
}

Now this code works, I can store the follower count in variable like so: $twitterfollow = getTwitterFollowers('dadalife'); 现在这段代码起作用了,我可以将关注者计数存储在变量中,如下所示:$ twitterfollow = getTwitterFollowers('dadalife');

Good luck to anyone else struggling with Twitter's new absurd API 祝其他人在Twitter的新荒谬API上苦苦挣扎

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM