gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 11:37:50 30 4
gpt4 key购买 nike

由于 Twitter API 1.0 自 June 11th 2013 起停用,下面的脚本不再起作用了。

// Create curl resource 
$ch = curl_init();
// Set url
curl_setopt($ch, CURLOPT_URL, "http://twitter.com/statuses/user_timeline/myscreenname.json?count=10");
// Return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$output = curl_exec($ch);
// Close curl resource to free up system resources
curl_close($ch);

if ($output)
{
$tweets = json_decode($output,true);

foreach ($tweets as $tweet)
{
print_r($tweet);
}
}

如何以尽可能少的代码获取 user_timeline(最近的状态)?

我发现了这个:https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline但我收到以下错误:

"{"errors":[{"message":"Could not authenticate you","code":32}]}"

那里有很多类(class),但是在尝试了几个之后,由于 Twitter 上的这些更新,它们似乎都不起作用,而且其中一些是非常高级的类(class),具有很多我并不真正需要的功能。

用 PHP 获取最近用户状态的最简单/最短的方法是什么?

最佳答案

Important Note: As of mid-2018, the process to get twitter API tokens became a lot more bureaucratic. It has taken me over one working week to be provided a set of API tokens, and this is for an open source project for you guys and girls with over 1.2 million installations on Packagist and 1.6k stars on Github, which theoretically should be higher priority.

If you are tasked with working with the twitter API for your work, you must take this potentially extremely long wait-time into account. Also consider other social media avenues like Facebook or Instagram and provide these options, as the process for retrieving their tokens is instant.


所以你想使用 Twitter v1.1 API?

Note: the files for these are on GitHub .

版本 1.0 will soon be deprecated并且不允许未经授权的请求。所以,这里有一篇文章可以帮助您做到这一点,还有一个 PHP 类可以让您的生活更轻松。

1. Create a developer account: Set yourself up a developer account on Twitter

您需要访问官方 Twitter 开发者网站并注册开发者帐户。这是向 v1.1 API 发出请求的免费必要步骤。

2. Create an application: Create an application on the Twitter developer site

什么?您认为您可以提出未经身份验证的请求吗?不适用于 Twitter 的 v1.1 API。您需要访问http://dev.twitter.com/apps并单击“创建应用程序”按钮。

Enter image description here

在此页面上,填写您想要的任何详细信息。对我来说,这并不重要,因为我只是想发出大量阻止请求以摆脱垃圾邮件追随者。关键是您将为自己获得一组唯一键以用于您的应用程序。

因此,创建应用程序的目的是为自己(和 Twitter)提供一组 key 。它们是:

  • 消费者 key
  • 消费者的 secret
  • 访问 token
  • 访问 token 密码

有一点信息here关于这些 token 的用途。

3. Create access tokens: You'll need these to make successful requests

OAuth请求一些 token 。所以你需要为你生成它们。

Enter image description here

点击底部的“创建我的访问 token ”。然后,一旦您再次滚动到底部,您将拥有一些新生成的 key 。您需要从该页面获取之前标记的四个 key 以进行 API 调用,因此请在某处记下它们。

4. Change access level: You don't want read-only, do you?

如果您想适本地使用此 API,如果您正在使用 GET 进行标准数据检索以外的任何操作,则需要将设置更改为读取和写入。请求。

Enter image description here

选择页面顶部附近的“设置”标签。

Enter image description here

授予您的应用读/写权限,然后点击底部的“更新”。

您可以read more about the applications permission model Twitter 在这里使用的。


5. Write code to access the API: I've done most of it for you

我将上面的代码,经过一些修改和更改,组合成一个 PHP 类,因此发出您需要的请求非常简单。

这使用 OAuthTwitter v1.1 API,以及我创建的类,您可以在下面找到。

require_once('TwitterAPIExchange.php');

/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
$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"
);

确保将您从上面的应用程序中获得的 key 放在各自的空间中。

接下来,您需要选择要向其发出请求的 URL。 Twitter 有他们的 API documentation 帮助您选择哪个 URL 以及请求类型(POST 或 GET)。

/** URL for REST request, see: https://dev.twitter.com/docs/api/1.1/ **/
$url = 'https://api.twitter.com/1.1/blocks/create.json';
$requestMethod = 'POST';

在文档中,每个 URL 都说明了您可以传递给它的内容。如果我们使用上面的“blocks” URL,我可以传递以下 POST 参数:

/** POST fields required by the URL above. See relevant docs as above **/
$postfields = array(
'screen_name' => 'usernameToBlock',
'skip_status' => '1'
);

现在您已经设置了要使用 API 执行的操作,是时候发出实际请求了。

/** Perform the request and echo the response **/
$twitter = new TwitterAPIExchange($settings);
echo $twitter->buildOauth($url, $requestMethod)
->setPostfields($postfields)
->performRequest();

对于 POST 请求,就是这样!

对于 GET 请求,情况略有不同。这是一个例子:

/** Note: Set the GET field BEFORE calling buildOauth(); **/
$url = 'https://api.twitter.com/1.1/followers/ids.json';
$getfield = '?username=J7mbo';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
echo $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();

最终代码示例:对于我的关注者列表的简单 GET 请求。

$url = 'https://api.twitter.com/1.1/followers/list.json';
$getfield = '?username=J7mbo&skip_status=1';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
echo $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();

I've put these files on GitHub 感谢@lackovic10 和@rivers!我希望有人觉得它有用;我知道我做到了(我将它用于循环中的批量阻塞)。

Also, for those on Windows who are having problems with SSL certificates, look at this post. This library uses cURL under the hood so you need to make sure you have your cURL certs set up probably. Google is also your friend.

关于php - 使用 Twitter API 1.1 版检索 user_timeline 的最简单 PHP 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12916539/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com