繁体   English   中英

PHP-如何从Twitter REST API获取推文计数,关注计数,关注者计数以及喜欢计数?

[英]PHP - How to get tweet count, following count, follower count and like count from Twitter REST api?

我有以下代码从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”,点赞=“ favourites_count”

问题是我无法解决如何修改代码以调用所有4个变量的问题。 我确实尝试过只是重复GET请求并更改变量名,但是它似乎没有用。

任何帮助将不胜感激!

更新:

非常感谢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

?>

一旦从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

?>

这样就完成了。 希望这可以帮助 :)

暂无
暂无

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

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