无法使用 Twitter API 1.1 和 PHP 获取最新推文

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13862299/
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 06:11:57  来源:igfitidea点击:

Can't get latest tweets using Twitter API 1.1 and PHP

phpcurltwittertwitter-oauth

提问by Javier Villanueva

I'm trying to get the latest tweets from a particular user using PHP and CURL and the new 1.1 API, I created my new App in the twitter website and went to the OAuth tool tab to generate the signature and according to the page it generates the following curl command:

我正在尝试使用 PHP 和 CURL 以及新的 1.1 API 获取来自特定用户的最新推文,我在 twitter 网站上创建了我的新应用程序并转到 OAuth 工具选项卡以生成签名并根据它生成的页面以下 curl 命令:

curl --get 'https://api.twitter.com/1.1/statuses/user_timeline.json?count=2&screen_name=twitter' --header 'Authorization: OAuth oauth_consumer_key="Waqd223QDuge6l9UboBldg", oauth_nonce="8a9beae863838b0ba2bc6ac03bc9757e", oauth_signature="u2PrZ5g14HGb39pHoYl9azHp6vg%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1355408402", oauth_token="171967067-lyLujNed9QyVBfmlfAkFXiUt5KIQkqrCMwg6utFc", oauth_version="1.0"'

Which works fine in my terminal, however when I try to replicate it in PHP I get a "Could not authenticate you" error, this is what I have so far:

这在我的终端中运行良好,但是当我尝试在 PHP 中复制它时,我收到“无法对您进行身份验证”错误,这是我目前所拥有的:

$options = array(
    URLOPT_HTTPHEADER      => array('Authorization: OAuth oauth_consumer_key="Waqd223QDuge6l9UboBldg", oauth_nonce="8a9beae863838b0ba2bc6ac03bc9757e", oauth_signature="toLnir5cHUUvEj8X29SdzjlOTXc%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="' . time() . '", oauth_token="171967067-lyLujNed9QyVBfmlfAkFXiUt5KIQkqrCMwg6utFc", oauth_version="1.0"'),
    CURLOPT_HEADER         => false,
    CURLOPT_URL            => 'https://api.twitter.com/1.1/statuses/user_timeline.json?count=2&screen_name=twitter',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_SSL_VERIFYPEER => false
);

$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
curl_close($feed);

$twitter_data = json_decode($json);

var_dump($twitter_data);

The only thing I'm changing is the oauth_timestampwhere I use the time()function to get the current time, I figure I must be passing the wrong options to the curl function but I can't figure it out.

我唯一要改变的是oauth_timestamp我使用该time()函数获取当前时间的地方,我想我必须将错误的选项传递给 curl 函数,但我无法弄清楚。

Thanks in advance.

提前致谢。

回答by lackovic10

Here you can find an example of getting tweets with the twitter api 1.1 and curl

在这里您可以找到使用 twitter api 1.1 和 curl 获取推文的示例

Simplest PHP example for retrieving user_timeline with Twitter API version 1.1

使用 Twitter API 1.1 版检索 user_timeline 的最简单 PHP 示例

回答by skaterdav85

Took me a while to get something working but I finally did and have pushed a standalone class and demo to GitHub that should be pretty easy to follow.

我花了一段时间才开始工作,但我终于做到了,并将一个独立的类和演示推送到 GitHub,这应该很容易理解。

https://github.com/skaterdav85/twitter-library

https://github.com/skaterdav85/twitter-library

回答by Albert Koz?owski

Really simple php wrapper for Twitter 1.1 rest api

用于 Twitter 1.1 rest api 的非常简单的 php 包装器

https://github.com/vojant/Twitter-php

https://github.com/vojant/Twitter-php