PHP - 如何从Twitter REST api获取推文数,跟随计数,跟随者数和类似数量?

时间:2017-05-10 18:52:29

标签: php twitter

我有以下代码从twitter api请求关注者计数。

<?php  

/* 
* Requires the "Twitter API" wrapper by James Mallison 
* to be found at https://github.com/J7mbo/twitter-api-php
*
* The way how to get a follower count was posted by Amal Murali
* on http://stackoverflow.com/questions/17409227/follower-count-number-in-twitter
*/

require_once('twitterapi/TwitterAPIExchange.php');              // adjust server path accordingly

// GET YOUR TOKENS AND KEYS at https://dev.twitter.com/apps/
$settings = array(
'oauth_access_token' => "SECRET",               // enter your data here
'oauth_access_token_secret' => "SECRET",        // enter your data here
'consumer_key' => "SECRET",                 // enter your data here
'consumer_secret' => "SECRET"               // enter your data here
);

$ta_url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
$getfield = '?screen_name=SECRET';                  // enter your twitter name without the "@" here
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$follow_count=$twitter->setGetfield($getfield)
->buildOauth($ta_url, $requestMethod)
->performRequest();
$data = json_decode($follow_count, true);
$followers_count = $data[0]['user']['followers_count'];


?>

它工作得很好,但它只能得到追随者的数量。我也想要推特计数,跟计数,以及计数。

根据twitter api文档,我需要的文件全部来自https://api.twitter.com/1.1/statuses/user_timeline.json。他们被称为:

Tweets =“statuses_count”,关注=“friends_count”,Likes =“favourites_count”

问题是我无法弄清楚如何调整代码来调用所有4个变量。我确实尝试重复GET请求并更改变量名称,但似乎没有用。

非常感谢任何帮助!

UPDATE:

非常感谢OldPadawan,他的代码完美无缺。

这是最终代码的样子:

<?php  

/* 
* Requires the "Twitter API" wrapper by James Mallison 
* to be found at https://github.com/J7mbo/twitter-api-php
*
* The way how to get a follower count was posted by Amal Murali
* on http://stackoverflow.com/questions/17409227/follower-count-number-in-twitter
*/

require_once('twitterapi/TwitterAPIExchange.php');              // adjust server path accordingly

// GET YOUR TOKENS AND KEYS at https://dev.twitter.com/apps/
$settings = array(
'oauth_access_token' => "xxxxx",                // enter your data here
'oauth_access_token_secret' => "xxxxx",     // enter your data here
'consumer_key' => "Xxxxx",                  // enter your data here
'consumer_secret' => "xxxxx"                // enter your data here
);

$ta_url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
$getfield = '?screen_name=ConsoleRepairHQ'; // enter your twitter name without the "@" here
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$follow_count=$twitter->setGetfield($getfield)
->buildOauth($ta_url, $requestMethod)
->performRequest();

$data = json_decode($follow_count, true);

foreach($data as $tweets) { // we run through the array

$followers = $tweets['user']['followers_count'];
$friends = $tweets['user']['friends_count'];
$likes = $tweets['user']['favourites_count'];
$statuses = $tweets['user']['statuses_count'];

} // end of loop

?>

1 个答案:

答案 0 :(得分:1)

从Twitter获得json响应后,您将其视为数组并使用foreach

在您的情况下,在:

之后
$data = json_decode($follow_count, true);
  

您可以打印数组以查看它的外观(仅供参考,以后不需要),但它可以更容易理解逻辑和数据的存储位置(可以是不同的数组结构)

你需要的是添加这个:

<?php

$data = json_decode($follow_count, true);
print_r($data); // just checking, not needed later

  foreach($data as $tweets) { // we run through the array

    // here's an example of the structure and how you access it
    // compare to the printed array if you need more information

    $tweet = $tweets['text'];
    $name = $tweets['user']['name'];
    $turl = $tweets['user']['url'];
    $followers = $tweets['user']['followers_count'];
    $friends = $tweets['user']['friends_count'];
    $likes = $tweets['user']['favourites_count'];

    echo "Tweet: $tweet ($name / $turl) -> followed by : $followers -> friends: $friends -> likes: $likes<br />";
  } // end of loop

?>

你已经完成了。希望这会有所帮助:)