- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
由于 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.
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并单击“创建应用程序”按钮。
在此页面上,填写您想要的任何详细信息。对我来说,这并不重要,因为我只是想发出大量阻止请求以摆脱垃圾邮件追随者。关键是您将为自己获得一组唯一键以用于您的应用程序。
因此,创建应用程序的目的是为自己(和 Twitter)提供一组 key 。它们是:
有一点信息here关于这些 token 的用途。
3. Create access tokens: You'll need these to make successful requests
OAuth请求一些 token 。所以你需要为你生成它们。
点击底部的“创建我的访问 token ”。然后,一旦您再次滚动到底部,您将拥有一些新生成的 key 。您需要从该页面获取之前标记的四个 key 以进行 API 调用,因此请在某处记下它们。
4. Change access level: You don't want read-only, do you?
如果您想适本地使用此 API,如果您正在使用 GET 进行标准数据检索以外的任何操作,则需要将设置更改为读取和写入。请求。
选择页面顶部附近的“设置”标签。
授予您的应用读/写权限,然后点击底部的“更新”。
您可以read more about the applications permission model Twitter 在这里使用的。
5. Write code to access the API: I've done most of it for you
我将上面的代码,经过一些修改和更改,组合成一个 PHP 类,因此发出您需要的请求非常简单。
这使用 OAuth 和 Twitter 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/
在我们网站的新闻页面上,我们使用 Twitter 的 GET statuses/user_timeline API 端点为我们显示的 8 个新闻故事的每个 Ajax 页面提取 4 条推文,在结果网格中
我在将 Twitter API 集成到我的网站以制作我最新推文的一小部分时遇到问题。我使用此 URL 调用 JSON API: http://twitter.com/statuses/user_tim
需要一些帮助的人。现在,我可以在没有 Twitter 帐户的情况下成功获得单个用户时间线。但是我不知道如何在一个请求中获取多个用户时间线。实际上我只需要通过一个请求获得 3 个用户时间线。有什么帮助吗
已解决 问题是 Twitter 现在(愚蠢地)甚至对于公共(public)数据也需要 OAuth。一个足够简单的解决方法是使用本地 php 脚本进行 OAuth 并获取数据并使用 Ajax 访问数据,
我有一个应用程序,让用户通过 Twitter 进行身份验证,并让我的应用程序访问他们的 Twitter 帐户。为此,他们单击前端的一个按钮,前端通过使用应用程序信息访问 Twitter 来获取服务器上
除了已经返回的 favorite_count 和 retweet_count 之外,还有什么方法可以使用 GET statuses/user_timeline 获取推文的评论/回复计数? 谢谢! 最佳
我之前使用过一些 jQuery 来阅读 twitter 上的推文: $.ajax('https://api.twitter.com/1/statuses/user_timeline.json', {
使用 Twitter API,我可以获得所有用户的带有位置信息的推文。如果他们使用移动应用程序并附加照片,我如何从 API 获取该照片? 最佳答案 首先,您需要包含 include_entities=
你可能认为这是一个显而易见的问题,我不必问,但显然我这样做,因为在 documentation for this 中他们说了两件看似矛盾的事情。第一: This method can only ret
我正在为大量用户收集推文,因此该脚本将在无人监督的情况下运行数天/数周。我在 big_list 中有一个 user_id 列表。我认为一些推文是私有(private)的,我的脚本停止了,所以我想要一种
我使用 Bobby's answer获取Twitter的用户时间线,代码非常简单易懂,我在Windows/.NET 4.5下试过,效果很好。 但是当我尝试使用完全相同的代码在 Ubuntu/Mono
我正在使用 tweepy 使用包含的脚本从用户的时间轴中获取推文 here .然而,这些推文被截断了: auth = tweepy.OAuthHandler(consumer_key, consume
下面的 PHP 代码曾经工作然后今天早上突然调用了下面的代码 $get_tweets = $connection->get("https://api.twitter.com/1.1/statuses/
我希望提取推文中存在的外部 URL 的值。加上生成的该 URL 的缩略图。 示例推文: http://prntscr.com/ogdqey https://twitter.com/JarirBooks
似乎使用 tweepy 我只能使用 user_timeline 方法获得 200 条推文。 class Twitter_User(): def __init__(self,id,count=2
我正在为 android 制作一个应用程序,其功能之一是推文列表,我读取 Twitter api 请求的 json 警报的推文,我使用的是 1.0 版本的 api,但是每小时请求数限制为 150,很容
我一直在尝试实现 Fabric 以从用户那里获取 5 条最新推文的列表。它工作了几个小时,然后就停止工作了。我想在没有用户登录的情况下执行此操作,据我所知,API 允许访客登录阅读推文,但这可能对速率
前一段时间我有这个代码来放置用户的最后 3 条推文: jQuery.getJSON("http://twitter.com/statuses/user_timeline/"+ user +".json
由于 Twitter API 1.0 于 2013 年 6 月 11 日停用,我的旧脚本不再有效。 如何以尽可能少的编码获取 user_timeline(最近的状态)? 最佳答案 使用 Jason 提
我正在尝试创建一个包含 twitter api、facebook api 和 instagram api 的表的数据库。 这是我尝试过的 foreach($string as $items)
我是一名优秀的程序员,十分优秀!