正如上面评论中所解释的,twitter API 的第 1 版已被弃用,并将很快被完全删除。这意味着简单的请求将不再起作用一旦它被移除,这很快就会成为现实!
基本上,从那时起,为了从 Twitter 获取任何类型的数据,你需要制作经过身份验证的请求使用他们的版本1.1 api.
我在这里写了一篇文章 https://stackoverflow.com/questions/12916539/simplest-php-example-retrieving-user-timeline-with-twitter-api-version-1-1/15314662#15314662其中为需要的人提供了漂亮的图片,解释了使用 PHP 调用其 1.1 版本 api 所需的确切步骤。
在您彻底阅读完上述文章后,这就是您想要做的事情。
- 在 Twitter 开发站点上设置一个应用程序。这很简单,你和 Twitter 之间都有一组键(总共有四个键,我的帖子解释了这一点)。
- 包括 PHP 脚本,所有内容都在我的帖子和 github 上。
- 从那时起,您只需将变量用户名传递给 PHP,并使用该变量通过 v1.1 api 检查它们是否存在。
我假设您了解一点 PHP。这就是您将如何使用推特 v1.1 API https://github.com/J7mbo/twitter-api-php检查用户名是否存在。
看一下文档 https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline用于获取用户的时间表。文档说明你可以使用 screen_name 参数。您还知道它需要获取请求.
有了上述信息和我的课程,您就可以非常轻松地为用户执行请求。
require_once "TwitterAPIExchange.php";
// As my post explains, these are the keys you get from the twitter dev site
$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"
);
// This is the username you want to check. You can get it however you want. Just place it into this $username variable.
$username = 'USERNAMEHERE';
$url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
$requestMethod = 'GET';
$getfield = '?screen_name='.$username;
$twitter = new TwitterAPIExchange($settings);
$result = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
好的,结果现在存储在$result
多变的。你可以在这里做你想做的事。您想检查用户是否存在,所以var_dump()
结果并寻找如何确定用户是否存在。显然它不会包含实际用户,它可能包含false
or null
.
因为我根本不知道,所以可以说$result->user
等于false
。你只需这样做:
if (!$result->user)
{
echo "The user doesn't exist!";
}
else
{
echo "The user exists!";
}
...或者,速记风格(我喜欢在我的帖子中添加这样的小东西)
echo (!$result->user) ? "Doesn't exist! :(" : "Exists!";
做一些研究,使用:
- 我的帖子在这里 https://stackoverflow.com/questions/12916539/simplest-php-example-retrieving-user-timeline-with-twitter-api-version-1-1/15314662#15314662
- github上的类 https://github.com/J7mbo/twitter-api-php
- Twitter 开发 API https://developer.twitter.com/en/docs