Twitter 通过主题标签搜索示例 API v1.1 [英] Twitter search by hashtag example API v1.1

查看:33
本文介绍了Twitter 通过主题标签搜索示例 API v1.1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去,使用 Twitter API 版本 1,我使用以下 URL 获取所有带有主题标签棒球"的推文的 JSON 提要:

http://search.twitter.com/search.json?q=%23baseball&result_type=recent

您如何使用 API 1.1 版获得类似的结果?我使用 PHP 作为服务器端代码,所以不确定是否需要使用它来进行身份验证等?

示例代码会非常有帮助.谢谢.

解决方案

如您所知,现在需要经过身份验证的请求,因此您可能需要先了解一些事项.新的 1.1 搜索、如何使用主题标签和身份验证.

Twitter 搜索 1.1

可以在此处找到新的 Twitter 搜索 API 文档.根据这些文档:

https://api.twitter.com/1.1/search/tweets.json 是用于搜索的新资源 URL.

标签搜索

你说得对!%23 解码为 # 字符.

身份验证

OAuth 要复杂得多.如果你只是使用一个刚刚工作的库,那会有所帮助.

这里是 一个帖子 很多人发现对帮助您向 1.1 API 发出经过身份验证的请求很有帮助.这包括一个包含 的单一文件,以发出您需要的请求.>

示例

此示例假设您正在使用上述库并设置您的密钥等.要提出您的请求:

//您的具体要求$url = 'https://api.twitter.com/1.1/search/tweets.json';$requestMethod = 'GET';$getfield = '?q=#baseball&result_type=recent';//执行请求$twitter = new TwitterAPIExchange($settings);echo $twitter->setGetfield($getfield)->buildOauth($url, $requestMethod)->performRequest();

是的,就是这样.除了您需要做的一些小设置(正如我的帖子所解释的),对于您的开发密钥,这就是您执行经过身份验证的请求所需的一切.

回复

响应以 JSON 格式返回给您.来自概述:

<块引用>

API v1.1 将仅支持 JSON.一段时间以来,我们一直在暗示这一点,首先是在 Streaming API 和最近的趋势 API 上放弃 XML 支持.我们选择支持跨平台共享的 JSON 格式.

In the past, using Twitter API version 1, I used the following URL to get a JSON feed of all tweets with the hashtag "baseball":

http://search.twitter.com/search.json?q=%23baseball&result_type=recent

How do you achieve a similar result using API version 1.1? I'm using PHP as my server-side code, so not sure if I need to use it to authenticate and such?

Sample code would be extremely helpful. Thanks.

解决方案

As you know, authenticated requests are now required, so there's a few things that you may need to take a look at first. The new 1.1 search, how to use hashtags, and authentication.

Twitter Search for 1.1

The new twitter search api docs can be found here. According to these docs:

https://api.twitter.com/1.1/search/tweets.json is the new resource URL to use for search.

Hashtag searches

You've got that part right! %23 decodes to a # character.

Authentication

OAuth is a lot more complex. It would help if you just used a library that just worked.

Here's a post a lot of people found useful to help you make authenticated requests to the 1.1 API. This includes a one-file include library to make requests like those you require.

Example

This example assumes you're using the above library and set up your keys etc. To make your request:

// Your specific requirements
$url = 'https://api.twitter.com/1.1/search/tweets.json';
$requestMethod = 'GET';
$getfield = '?q=#baseball&result_type=recent';

// Perform the request
$twitter = new TwitterAPIExchange($settings);
echo $twitter->setGetfield($getfield)
             ->buildOauth($url, $requestMethod)
             ->performRequest();

Yes, that's it. Apart from the little setting up you need to do (as my post explains), for your dev keys, that's everything you need to perform authenticated requests.

Response

The response is returned to you in JSON. From the overview:

API v1.1 will support JSON only. We've been hinting at this for some time now, first dropping XML support on the Streaming API and more recently on the trends API. We've chosen to throw our support behind the JSON format shared across the platform.

这篇关于Twitter 通过主题标签搜索示例 API v1.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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