Follower count number in Twitter(Twitter 中的关注者数量)
问题描述
如何使用 PHP 获取我的粉丝数.
How to get my followers count number with PHP.
我在这里找到了这个答案: Twitter 粉丝计数,但它不起作用,因为API 1.0 不再有效.
I found this answer here: Twitter follower count number, but it is not working because API 1.0 is no longer active.
我也尝试过使用此 URL 的 API 1.1:https://api.twitter.com/1.1/users/lookup.json?screen_name=google 但显示错误(错误的身份验证数据).
I have also tried with API 1.1 using this URL: https://api.twitter.com/1.1/users/lookup.json?screen_name=google but is is showing an error(Bad Authentication data).
这是我的代码:
$data = json_decode(file_get_contents('http://api.twitter.com/1.1/users/lookup.json?screen_name=google'), true);
echo $data[0]['followers_count'];
推荐答案
Twitter API 1.0 已弃用且不再处于活动状态.使用 REST 1.1 API,您需要 oAuth 身份验证才能从 Twitter 检索数据.
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.
改用这个:
<?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 可能更容易.
Parsing the XML might be easier in some cases.
这是一个解决方案(经过测试):
<?php
$xml = new SimpleXMLElement(urlencode(strip_tags('https://twitter.com/users/google.xml')), null, true);
echo "Follower count: ".$xml->followers_count;
?>
希望这有帮助!
这篇关于Twitter 中的关注者数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Twitter 中的关注者数量


基础教程推荐
- php中的PDF导出 2022-01-01
- 将变量从树枝传递给 js 2022-01-01
- Yii2 - 在运行时设置邮件传输参数 2022-01-01
- PHPUnit 的 Selenium 2 文档到底在哪里? 2022-01-01
- Web 服务器如何处理请求? 2021-01-01
- 如何在数学上评估像“2-1"这样的字符串?产生“1"? 2022-01-01
- 使用 scandir() 在目录中查找文件夹 (PHP) 2022-01-01
- php中的foreach复选框POST 2021-01-01
- 主题化 Drupal 7 的 Ubercart “/cart"页 2021-01-01
- php 7.4 在写入变量中的 Twig 问题 2022-01-01