php Twitter 中的关注者数量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17409227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Follower count number in Twitter
提问by Enve
How to get my followers count number with PHP.
如何使用 PHP 让我的追随者计数。
I found this answer here: Twitter follower count number, but it is not working because API 1.0 is no longer active.
我在这里找到了这个答案:Twitter follower count number,但它不起作用,因为 API 1.0 不再处于活动状态。
I have also tried with API 1.1 using this URL: https://api.twitter.com/1.1/users/lookup.json?screen_name=googlebut is is showing an error(Bad Authentication data).
我还尝试使用以下 URL 使用 API 1.1:https: //api.twitter.com/1.1/users/lookup.json?screen_name=google但显示错误(错误的身份验证数据)。
Here is my code:
这是我的代码:
$data = json_decode(file_get_contents('http://api.twitter.com/1.1/users/lookup.json?screen_name=google'), true);
echo $data[0]['followers_count'];
回答by cMinor
If do it withoutauth (replace 'stackoverflow' with your user)
如果没有身份验证(用您的用户替换“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']);
});
with php
用 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'];
回答by Amal Murali
Twitter API 1.0 is deprecated and is no longer active. With the REST 1.1 API, you need oAuth authentication to retrieve data from Twitter.
Twitter API 1.0 已弃用,不再处于活动状态。使用REST 1.1 API 时,您需要 oAuth 身份验证才能从 Twitter 检索数据。
Use this instead:
改用这个:
<?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;
?>
Parsing the XML might be easier in some cases.
在某些情况下,解析 XML 可能更容易。
Here's a solution (tested):
这是一个解决方案(已测试):
<?php
$xml = new SimpleXMLElement(urlencode(strip_tags('https://twitter.com/users/google.xml')), null, true);
echo "Follower count: ".$xml->followers_count;
?>
Hope this helps!
希望这可以帮助!