繁体   English   中英

推特中的追随者数量

[英]Follower count number in Twitter

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

我在这里找到了这个答案: Twitter关注者计数 ,但它不起作用,因为API 1.0不再有效。

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

这是我的代码:

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

如果没有 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 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;
?>

希望这可以帮助!

暂无
暂无

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

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