php Twitter 通过主题标签搜索示例 API v1.1

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17097357/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:15:03  来源:igfitidea点击:

Twitter search by hashtag example API v1.1

phptwitter

提问by John

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":

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

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

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?

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

Sample code would be extremely helpful. Thanks.

示例代码将非常有帮助。谢谢。

回答by Jimbo

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.

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

Twitter Search for 1.1

Twitter 搜索 1.1

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

可以在此处找到新的 twitter 搜索 api 文档。根据这些文档:

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

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

Hashtag searches

标签搜索

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

你说对了那部分!%23解码为一个#字符。

Authentication

验证

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

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

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

这是一篇很多人发现的帖子,可帮助您向 1.1 API 发出经过身份验证的请求。这包括一个单文件包含库,用于发出您需要的请求。

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:

响应以 JSON 形式返回给您。从概述

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.

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

回答by guilhermebr

If you just want to test, you can do the follow:

如果您只是想测试,可以执行以下操作:

Access the twitter dev console: https://dev.twitter.com/console

访问 twitter 开发控制台:https: //dev.twitter.com/console

In Authentication put: OAuth 1, that will ask you to give permission from your twitter account.

在 Authentication put: OAuth 1 中,这将要求您从您的 twitter 帐户中授予权限。

Request URL put GET

请求 URL put GET

In url: https://api.twitter.com/1.1/search/tweets.json?q=%23yourhashtag

在网址中:https: //api.twitter.com/1.1/search/tweets.json?q =%23yourhashtag

After Send, in Request window, copy the Authorization value.

发送后,在请求窗口中,复制授权值。

Now put it in your request header.

现在把它放在你的请求头中。

Go example:

去例子:

func main() {
    client := &http.Client{}
    req, _ := http.NewRequest("GET", "https://api.twitter.com/1.1/search/tweets.json?q=%23golang", nil)
    req.Header.Add("Authorization", `OAuth oauth_consumer_key=...`)

    resp, _ := client.Do(req)
    io.Copy(os.Stdout, resp.Body)
}

回答by thakis

Here's a simple example in python using application-only auth using the requests API. Get keys by creating an app at https://apps.twitter.com/app/new.

这是使用请求 API 使用仅应用程序身份验证的 Python 中的简单示例。通过在https://apps.twitter.com/app/new创建应用程序来获取密钥。

api_key = ...
api_secret = ...

# https://dev.twitter.com/oauth/application-only
# The base64 stuff described there is the normal Basic Auth dance.
import requests
r = requests.post('https://api.twitter.com/oauth2/token',
                  auth=(api_key, api_secret),
                  headers={'Content-Type':
                      'application/x-www-form-urlencoded;charset=UTF-8'},
                  data='grant_type=client_credentials')
assert r.json()['token_type'] == 'bearer'
bearer = r.json()['access_token']

url = 'https://api.twitter.com/1.1/search/tweets.json?q=%23yourhashtag'
r = requests.get(url, headers={'Authorization': 'Bearer ' + bearer})
print r.json()