【问题标题】:Follower count number in TwitterTwitter 中的关注者数量
【发布时间】:2013-06-28 20:31:06
【问题描述】:

如何使用 PHP 获取我的关注者人数。

我在这里找到了这个答案: Twitter follower count number,但它不起作用,因为 API 1.0 不再有效。

我也尝试使用此 URL:https://api.twitter.com/1.1/users/lookup.json?screen_name=google 使用 API 1.1,但显示错误(错误的身份验证数据)。

这是我的代码:

$data = json_decode(file_get_contents('http://api.twitter.com/1.1/users/lookup.json?screen_name=google'), true);
echo $data[0]['followers_count'];

【问题讨论】:

  • 您现在需要在 v1.1 中使用 oauth 连接
  • 我该如何使用它?

标签: php twitter twitter-follow


【解决方案1】:

Twitter API 1.0 已弃用且不再有效。使用 REST 1.1 API,您需要 oAuth 身份验证才能从 Twitter 检索数据。

改用这个:

<?php 
require_once('TwitterAPIExchange.php'); //get it from https://github.com/J7mbo/twitter-api-php

/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
$settings = array(
'oauth_access_token' => "YOUR_OAUTH_ACCESS_TOKEN",
'oauth_access_token_secret' => "YOUR_OAUTH_ACCESS_TOKEN_SECRET",
'consumer_key' => "YOUR_CONSUMER_KEY",
'consumer_secret' => "YOUR_CONSUMER_SECRET"
);

$ta_url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
$getfield = '?screen_name=REPLACE_ME';
$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'];
echo $followers_count;
?>

在某些情况下解析 XML 可能更容易。

这是一个解决方案(经过测试):

<?php 
$xml = new SimpleXMLElement(urlencode(strip_tags('https://twitter.com/users/google.xml')), null, true);
echo "Follower count: ".$xml->followers_count;
?>

希望这会有所帮助!

【讨论】:

  • 小错字,replace me 后少了一个单引号
  • @Novocaine88:谢谢。固定:)
  • 谢谢,但我找到了这个twitter.com/users/show/google。是否有任何简短的 PHP 代码来获取关注者数量?
  • 它显示这些错误:jsfiddle.net/enve/XYcTX(我不能在这里写评论,因为它太长了)
  • 嗨,上面的代码不起作用。从 json 回显时它什么也没显示。有什么想法吗?
【解决方案2】:

如果这样做没有 auth(用你的用户替换'stackoverflow')

$.ajax({
    url: "https://cdn.syndication.twimg.com/widgets/followbutton/info.json?screen_names=stackoverflow"
    dataType : 'jsonp',
    crossDomain : true
}).done(function(data) {
    console.log(data[0]['followers_count']);
});

用php

$tw_username = 'stackoverflow'; 
$data = file_get_contents('https://cdn.syndication.twimg.com/widgets/followbutton/info.json?screen_names='.$tw_username); 
$parsed =  json_decode($data,true);
$tw_followers =  $parsed[0]['followers_count'];

【讨论】:

  • 如果您只需要没有身份验证的简单关注者计数/数据,则此答案是最好的!优秀!编辑:但 twitter 不支持 - 如有更改,恕不另行通知。
  • 您也可以使用用户 ID 来获取信息,例如https://cdn.syndication.twimg.com/widgets/followbutton/info.json?user_ids=128700677
猜你喜欢
  • 2013-07-01
  • 1970-01-01
  • 2011-09-20
  • 1970-01-01
  • 1970-01-01
  • 2013-06-11
  • 1970-01-01
  • 2019-09-23
  • 1970-01-01
相关资源
最近更新 更多