【问题标题】:PHP - How to get tweet count, following count, follower count and like count from Twitter REST api?PHP - 如何从 Twitter REST api 获取推文计数、关注计数、关注计数和喜欢计数?
【发布时间】:2017-05-18 19:34:38
【问题描述】:

我有以下代码从 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 获得。它们被称为:

推文 = “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 响应,您将看到数组的外观以及您可以访问哪些变量,不是吗?
  • 是的,我做到了。推文 = “statuses_count”,关注 = “friends_count”,喜欢 = “favourites_count”。我似乎无法让代码工作以访问 4 个变量。
  • 您是否尝试遍历响应数组?
  • 对不起,我不知道该怎么做
  • 不在我的电脑前面 :/ search SO -> 我已经回答了类似的问题,你在网站上有很多例子,使用 foreach 循环很容易

标签: php twitter


【解决方案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

?>

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

【讨论】:

  • 非常感谢 OldPadawan。我努力了几个小时才能让这个工作。如果您想看看,我已经用最终代码更新了原始问题。再次感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-15
  • 1970-01-01
  • 2014-04-26
  • 2016-08-03
  • 1970-01-01
  • 2020-04-18
相关资源
最近更新 更多