简体   繁体   English

使用PHP的Twitter REST API v1.1简单请求示例

[英]Twitter REST API v1.1 simple request example with PHP

If I have twitter consumer key and consumer secret key for my application, how can I request this GET request with PHP without using any external libraries? 如果我的应用程序具有Twitter使用者密钥和使用者秘密密钥,如何在不使用任何外部库的情况下使用PHP请求 GET请求?

Any good example will be appreciated by newbies to twitter REST API. 任何新手都会喜欢Twitter REST API的好例子。

Having only the consumer key and consumer secret you cannot make a successful GET Request. 仅具有使用者密钥和使用者秘密,您无法发出成功的GET请求。 You would need an Oauth Token (Access Token) and an Oauth Token Secret (Access Token) also. 您还需要一个Oauth令牌(访问令牌)和一个Oauth令牌秘密(访问令牌)。

To obtain an access token and an access token secret you need to either authenticate or, if the application is only for personal use - create an access token by going to the application details on the app's page. 要获取访问令牌访问令牌密钥,您需要进行身份验证,或者,如果该应用程序仅供个人使用,请通过转到该应用程序页面上的应用程序详细信息来创建访问令牌。

I'm going forward to explain how to obtain the tokens the hard way (for other people to be able to use your twitter app too). 我将继续解释如何以困难的方式获取令牌(其他人也可以使用您的Twitter应用程序)。 You need to authenticate to Twitter's API. 您需要通过Twitter的API进行身份验证。 For reference please see twitter's official how to sign in article. 供参考,请参阅Twitter的官方如何登录文章。 I'm trying to provide an easier to understand explanation based on this article. 我试图根据本文提供一个更容易理解的解释。

If you already have the oauth token and oauth token secret you can jump straight to How to make a GET Request below. 如果您已经拥有oauth令牌和oauth令牌密钥,则可以直接跳至下面的如何发出GET请求


How to Authenticate 如何认证

*Before we go any further I would like to state that you need to have cURL activated, to be able to properly send any request. *在继续之前,我想声明您需要激活cURL才能正确发送任何请求。

Step 1 - Build the headers and send them to twitter to request an authentication token.(It basically authorizes you to authenticate) 第1步 -构建标头并将其发送到Twitter以请求身份验证令牌(它基本上授权您进行身份验证)

These are the headers you need to have: 这些是您需要的标题:

oauth_consumer_key = YOUR_CONSUMER_SECRET oauth_consumer_key = YOUR_CONSUMER_SECRET

oauth_nonce = RANDOM_GENERATED_32_CHAR_STRING oauth_nonce = RANDOM_GENERATED_32_CHAR_STRING

oauth_timestamp = NOW() oauth_timestamp = NOW()

oauth_callback = YOUR_CALLBACK oauth_callback = YOUR_CALLBACK

oauth_signature_method = HMAC-SHA1 oauth_signature_method = HMAC-SHA1

oauth_version = 1.0 oauth_version = 1.0

oauth_signature = the generated signature based on all of the above headers. oauth_signature =基于上述所有标头生成的签名。 (How to build a signature is a pretty big subject itself, so I would sugest to read this article .) (如何构建签名本身是一个相当大的主题,因此,我很推荐阅读本文 。)

After you have all the headers, sort them alphabetically , concatenate them in pairs of key and value (eg consumer_key="YOU_CONSUMER_KEY" ) and append them to the string "OAuth ". 在拥有所有标题之后,将它们按字母顺序排序它们按键和值对连接 (例如,consumer_key =“ YOU_CONSUMER_KEY”),然后将其附加到字符串“ OAuth”。

You will end up having something like: 您最终会遇到类似:

OAuth oauth_consumer_key="YOUR_CONSUMER_KEY", oauth_nonce="YOUR_NONCE", oauth_signature="YOUR_SIGNATURE", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1377125526", oauth_token="84832050-OHhyESU4wM2YGRINsseuJQwouG1PY1Rs5YReZH2wh", oauth_version="1.0" OAuth oauth_consumer_key =“ YOUR_CONSUMER_KEY”,oauth_nonce =“ YOUR_NONCE”,oauth_signature =“ YOUR_SIGNATURE”,oauth_signature_method =“ HMAC-SHA1”,oauth_timestamp =“ 1377125526”,oauth_token =“ 84832050-OHhyESU4wM2YG

This is your header value. 这是您的标头值。

Send an ' Authorization ' header with this value via POST using cUrl to this url: https:// + api.twitter.com/oauth/request_token (*take the '+' out) and store the response in a var. 使用cUrl通过POST通过POST发送带有此值的' Authorization '标头到以下URL:https:// + api.twitter.com/oauth/request_token(*取出'+')并将响应存储在var中。

If everything went fine your storing var should be populated with a string containing your oauth_token. 如果一切顺利,则应使用包含oauth_token的字符串填充存储的var。 *Note this is not you access token - it is just the token which you will use to authenticate. *请注意,这不是您访问令牌-只是用于认证的令牌。

Now that you have the oauth_token value you can redirect to https:// + api.twitter.com/oauth/authenticate?oauth_token=THE_RECEIVED_OAUTH_TOKEN_VALUE 现在您有了oauth_token值,您可以重定向到https:// + api.twitter.com/oauth/authenticate?oauth_token=THE_RECEIVED_OAUTH_TOKEN_VALUE

At this point you are prompted with a log in form on twitter, and you are ready to move to 此时,系统会提示您登录Twitter,并准备移至

STEP 2 - THE CALLBACK 第2步-回调

After you logged in with twitter in the previous step you were redirected to you http:// + OWN_CALLBACK_URL?/oauth_token=SOME_VALUE&oauth_verifier=SOME_VALUE 在上一步中使用Twitter登录后,您将重定向到http:// + OWN_CALLBACK_URL?/ oauth_token = SOME_VALUE&oauth_verifier = SOME_VALUE

* Again these are not you final access tokens. * 同样,这些不是您的最终访问令牌。 You just need to use them to create your new header and send them back to twitter. 您只需要使用它们来创建新的标题并将它们发送回Twitter。

STEP 3 - Exchange the token 步骤3-交换令牌

So, append these 2 tokens to the list of headers already created in the STEP 1, rebuild the signature based on all of the headers and send another Authorization header with the new header value created via POST using cURL . 因此,将这两个标记附加到已在STEP 1中创建的标头列表中,基于所有标头 重建签名并使用cURL通过POST通过发送的新标头值发送另一个Authorization标头。

If everything went fine again, The response of the this cURL should be another string containing the oauth_token and oauth_token_secret. 如果一切正常,则此cURL的响应应该是另一个包含oauth_token和oauth_token_secret的字符串。 *These are your real access token and access token secret. *这些是您的真实访问令牌和访问令牌密钥。

Once you have the real oauth_token(I call it access_token) and oauth_token_secret you can store them in the DB, in SESSION or wherever you want. 一旦有了真正的oauth_token(我称其为access_token)和oauth_token_secret,就可以将它们存储在数据库中,SESSION中或任何需要的地方。 You will need them for each GET request from now on. 从现在开始,每个GET请求都将需要它们。



How to make a GET Request 如何发出GET请求

*Before we go any further I would like to state that you need to have cURL activated, to be able to properly send any request. *在继续之前,我想声明您需要激活cURL才能正确发送任何请求。

You need to know the endpoint url you want to make the request to. 您需要知道要向其发出请求的终结点URL。 (eg if you would like to get all your favorite tweets you need this url: https + ://api.twitter.com/1.1/favorites/list.json (take the ' + ' out) (例如,如果您想获取所有喜欢的推文,则需要以下网址:https +://api.twitter.com/1.1/favorites/list.json(删除“ +”)

Create the headers 创建标题

You will need to create the set of headers (please see STEP 1 in How to Authenticate on how to create the headers) and append the oauth token to them. 您将需要创建标头集(请参见如何验证标头中的步骤1, 了解如何创建标头)并将oauth令牌附加到标头集。

* NOTE When you create the signature you will need to have the oauth token secret + the default ouath consumer secret to sign the base string. * 注意创建签名时,您将需要具有oauth令牌密钥+默认的ouath消费者密钥以对基本字符串进行签名。 For more info please see https + ://dev.twitter.com/docs/auth/creating-signature (take the ' + ' out) on how to build a signature. 有关更多信息,请参见https +://dev.twitter.com/docs/auth/creating-signature(取出“ +”)有关如何构建签名的信息。

* NOTE 2 You will also need to have all the extra query params (whatever is after list.json in the endpoint URL) when building the signature. * 注2:在构建签名时,您还需要具有所有额外的查询参数(无论端点URL中list.json之后是什么)。


Send the Request via GET http method to the endpoint url with the 'Authorization" header just created using cURL and store the result in a local var. 通过GET http方法将请求发送到带有使用cURL创建的“ Authorization”标头的端点 url,并将结果存储在本地var中。

var_dump that var and see the goodies it contains. var_dump该var并查看其中包含的好东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM