【发布时间】: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 循环很容易