Twitter API:Url 搜索用户名 [英] Twitter API: Url search for Username

查看:39
本文介绍了Twitter API:Url 搜索用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网站列表,我想查看它们是否有 Twitter 帐户.我很好奇 API 中是否有针对用户名的 url 搜索,或者类似的东西.我一直在阅读和环顾四周;然而,我来了.当我可以运行一个函数来为我完成工作时,我不想手动执行此操作.非常感谢有关此主题的一些反馈.

I have a list of websites where I want to see if they have twitter accounts. I was curious if there is a url search for username in the API, or something of this nature. I've been reading and looking around; however, I've come up short. I would hate to have to do this manually when I could run a function to do the work for me. Would greatly appreciate some feedback on this topic.

美好的一天!

推荐答案

如上面评论中所述,twitter API 的第 1 版已弃用,很快将被完全删除.这意味着简单的请求在被删除后将不再起作用,这很快就会成为现实!

As explained in the comment above, version 1 of the twitter API is deprecated and will soon be removed completely. This means that simple requests will not work any more once it has been removed, which is real soon!

基本上,从那时起,为了从 Twitter 获取任何类型的数据,您需要使用他们的 1.1 api 发出经过身份验证的请求.

Basically, from then on, in order to get any sort of data from twitter, you need to make authenticated requests using their version 1.1 api.

我在这里写了一篇文章,其中解释了使用 PHP 调用其 1.1 版 api 所需的确切步骤,并为需要它们的人提供了漂亮的图片.

I wrote a post here which explains, with pretty pictures for those who need them, the exact steps required to make calls to their version 1.1 api using PHP.

在您彻底阅读上述帖子后,这就是您想要做的事情.

Here's what you want to do, after you have read the above post thoroughly.

  1. 在 twitter 开发站点上设置一个应用程序.这很简单,所以您和 Twitter 之间都有一组键(总共有四个键,我的帖子对此进行了解释).
  2. 包括 PHP 脚本,所有内容都在我的帖子和 github 上.
  3. 从那时起,您只需将一个变量 username 传递给 PHP,并使用该变量通过 v1.1 api 检查它们是否存在.

我假设您了解一点 PHP.这是您如何使用 twitter v1.1 api 检查用户名是否存在与否.

I'm going to assume you know a little PHP. This is how you would use the twitter v1.1 api to check if a username exists or not.

查看文档以获得用户的时间线.文档说明您可以使用 screen_name 参数.您也知道它需要 GET 请求.

Take a look at the docs for getting a user's timeline. The docs state you can use a screen_name parameter. You also know it requires a GET request.

有了以上信息和我的班级,您就可以非常轻松地执行用户请求.

Armed with the above information, and my class, you can perform a request for a user pretty easily.

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() 结果并寻找如何确定用户是否存在.显然它不会包含实际用户,它可能包含 falsenull.

Okay, so the result is now stored in the $result variable. You can do what you want here. You want to check the user exists, so var_dump() the result and look for how to figure out if the user exists or not. Clearly it won't contain an actual user, it may contain false or null.

由于我不知道我的头顶,假设 $result->user 等于 false.您只需这样做:

As I don't know off the top of my head, lets say $result->user is equal to false. You would simply do this:

if (!$result->user)
{
    echo "The user doesn't exist!";
}
else
{
    echo "The user exists!";
}

...或者,简写风格(我喜欢在我的帖子中添加这样的小东西)

... or, short-hand style (I like adding little things like this to my posts)

echo (!$result->user) ? "Doesn't exist! :(" : "Exists!";

做一点研究,使用:

这篇关于Twitter API:Url 搜索用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆