Env*_*nve 9 php twitter twitter-follow
如何使用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'];
Run Code Online (Sandbox Code Playgroud)
cMi*_*nor 19
如果没有 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']);
});
Run Code Online (Sandbox Code Playgroud)
用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'];
Run Code Online (Sandbox Code Playgroud)
Ama*_*ali 16
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;
?>
Run Code Online (Sandbox Code Playgroud)
在某些情况下,解析XML可能会更容易.
这是一个解决方案(已测试):
<?php
$xml = new SimpleXMLElement(urlencode(strip_tags('https://twitter.com/users/google.xml')), null, true);
echo "Follower count: ".$xml->followers_count;
?>
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助!
| 归档时间: |
|
| 查看次数: |
20318 次 |
| 最近记录: |