引入 phirehose,它可以让您轻松使用 PHP 处理 Twitter Streaming API!

目录
你好。
我是Mandai,负责Wild 开发团队。
我不确定Twitter现在是否依然流行,或者它是否已经发展成熟,但我决定尝试使用
Twitter Streaming API从头开始编写程序会很麻烦,所以我尝试使用一个便捷的库,结果发现它非常简单易用,因此我想和大家分享一下。
获取 Twitter API 的访问令牌和访问密钥
没有它,一切都无法开始,所以您应该尽快获取一个。
您可以创建一个专用帐户,也可以以现有用户的身份获取一个帐户。
您登录 Twitter 并此页面,您会看到一个名为“创建新应用”的链接,请从那里注册。
Streaming API 对访问次数没有限制(或者更确切地说,由于它是流式传输,您可以持续连接并接收数据),因此无需担心因过度访问而被封禁。
但是,如果在通信失败的情况下多次重试,则可能会被阻止,因此您需要创建一个程序来处理重新连接和响应监控。
开发这个功能需要时间,所以这次我们将引入一个库来涵盖这方面的内容。
隆重推出 Firehose,它让 Streaming API 的使用变得轻松便捷。
phirehose是一个与 Twitter Streaming API 交互的 PHP 库。
它提供了处理从身份验证到连接和数据检索的所有类,因此您只需复制粘贴示例并填写访问密钥信息,然后从控制台运行它即可检索数据。
这是一个 GitHub 上的公共仓库,所以你可以直接通过 git clone 命令克隆它来使用它。
# 在项目目录下,执行 git clone https://github.com:fennb/phirehose.git
以上就是全部内容,接下来我想介绍一些与 Streaming API 端点对应的类。
公共流媒体
Public Streams 是一个 API,用于检索整个 Twitter 时间线的数据。
它有两个端点:
POST 状态/筛选
POST 状态/过滤器是使用 POST 方法定义的,但它们也会读取 GET 参数,这使得它们成为一个相当用户友好的 API。
使用以下三个标准缩小要阅读的推文范围:Twitter 用户 ID、关键词和位置信息。
关注(用户ID)
要筛选的用户 ID。
在 Firehose 中,此信息通过 setFollow() 方法以数组形式传递。
跟踪(关键词)
要筛选的关键字。
在 Firehose 中,通过 setTrack() 方法将它们作为数组传递。
地点
位置信息用于筛选。
选择是通过左下角和右上角两个点构成的矩形来实现的,因此,如果您只想筛选日本境内的位置,则需要连接多个位置。
在 Firehose 中,位置信息通过 `setLocations()` 方法以数组形式传递。
它还必须始终是一个二维数组。
- 左下角(最西南端)经度
- 左下角(最西南端)纬度
- 右上角(最东北端)经度
- 右上角(最东北角)纬度
参数是一个数组,其中包含按上述顺序排列的值。
因此,通过设置多个矩形,可以创建一个覆盖整个日本的区域。
您还可以筛选,只针对某些主要城市。
获取状态/示例
`GET statuses / sample`是一个 API,它会检索 Twitter 所有推文中随机选择的一小部分样本。
虽然它只捕获了大约 1% 的数据,但似乎仍然非常受欢迎,所以要谨慎使用。
没有其他选择。
用户流
这是一个针对单个用户的 API,用于检索其时间线、个人资料更新、事件等。
通过此 API 从数量不定的用户帐户进行流式传输可能会导致来自同一 IP 地址的连接受到限制,因此我们将考虑采用不同的方法。
用户流 API 还允许您将回复和转发添加到目标用户。
* 在phirehose中,有一个专门用于跟踪选项的方法,但似乎没有专门用于with和回复的方法。然而,由于我们这次没有使用用户流API,所以我们没有研究如何设置它,因此这里就不赘述了(这只是对Twitter提供的文档的翻译)。
(处理用户关注的其他帐户)
默认情况下,“with=followings”已设置,因此有关用户及其关注的用户的数据将包含在数据流中。
"with=user" 将仅包含帐户用户的数据。
回复(处理回复)
默认情况下,只有互为关注者的用户之间的回复才会被显示,但您可以通过指定“replies=all”来接收所有回复。
追踪(按关键词查找其他推文)
通过设置跟踪,您可以将与关键词匹配的其他推文添加到您的信息流中。
网站流
Site Stream API 目前处于 beta 测试阶段(而且已经持续了相当长一段时间),而 User Streams API 仅涵盖一个用户的时间线,但它将多个用户的时间线合并到一个流中。
由于是测试版,所以存在诸多限制,例如以下几点。它最终会正式发布吗?
- 单个连接可以为 100 个用户传输时间线(以及个人资料更新和其他数据)。使用控制流
- 每秒最多可建立 25 个连接。如果因过度调用等原因导致错误,必须实施指数退避机制。
- 如果你建立的连接超过1000个左右,就需要与Twitter平台团队协调测试和发布事宜。(我查了一下Twitter平台团队是什么意思,但没弄明白。它是Twitter内部的一个团队吗?我对此持怀疑态度,所以别太当真。)
可获取的 JSON 数据示例
以下是检索到的JSON数据示例。
每条数据都相当大,而且可以检索到各种各样的数据。
array(25) { ["created_at"]=> string(30) "Mon Mar 27 04:41:07 +0000 2017" ["id"]=> float(0.000000000000E+17) ["id_str"]=> string(18) "0000000000000000" ["text"]=> string(78) "xxxxxxxxxxxxxxx" ["source"]=> string(82) "xxxxxxxxxxxxxxxx" ["truncated"]=> bool(false) ["in_reply_to_status_id"]=> NULL ["in_reply_to_status_id_str"]=> NULL ["in_reply_to_user_id"]=> NULL ["in_reply_to_user_id_str"]=> NULL ["in_reply_to_screen_name"]=> NULL ["user"]=> array(38) { ["id"]=> float(0000000000) ["id_str"]=> string(10) "0000000000" ["name"]=> string(13) "xxx xxx" ["screen_name"]=> string(8) "xxxxxxxx" ["location"]=> string(27) "xxxxxxxxxxxxxxxx" ["url"]=> NULL ["description"]=> string(135) "xxxxxxxxxx" ["protected"]=> bool(false) ["verified"]=> bool(false) ["followers_count"]=> int(669) ["friends_count"]=> int(533) ["listed_count"]=> int(1) ["favourites_count"]=> int(2267) ["statuses_count"]=> int(3727) ["created_at"]=> string(30) "Fri Mar 20 09:23:52 +0000 2015" ["utc_offset"]=> NULL ["time_zone"]=> NULL ["geo_enabled"]=> bool(true) ["lang"]=> string(2) "ja" ["contributors_enabled"]=> bool(false) ["is_translator"]=> bool(false) ["profile_background_color"]=> string(6) "C0DEED" ["profile_background_image_url"]=> string(48) "xxxxxxxxxxxx" ["profile_background_image_url_https"]=> string(49) "xxxxxxxxxxxx" ["profile_background_tile"]=> bool(false) ["profile_link_color"]=> string(6) "1DA1F2" ["profile_sidebar_border_color"]=> string(6) "C0DEED" ["profile_sidebar_fill_color"]=> string(6) "DDEEF6" ["profile_text_color"]=> string(6) "333333" ["profile_use_background_image"]=> bool(true) ["profile_image_url"]=> string(74) "xxxxxxxxxx" ["profile_image_url_https"]=> string(75) "xxxxxxxxxx" ["profile_banner_url"]=> string(59) "xxxxxxxxxx" ["default_profile"]=> bool(true) ["default_profile_image"]=> bool(false) ["following"]=> NULL ["follow_request_sent"]=> NULL ["notifications"]=> NULL } ["geo"]=> NULL ["coordinates"]=> NULL ["place"]=> array(9) { ["id"]=> string(16) "5ab538af7e3d614b" ["url"]=> string(56) "https://api.twitter.com/1.1/geo/id/5ab538af7e3d614b.json" ["place_type"]=> string(4) "city" ["name"]=> string(16) "横滨市旭区" ["full_name"]=> string(23) "神奈川县横滨市旭区" ["country_code"]=> string(2) "JP" ["country"]=> string(6) "Japan" ["bounding_box"]=> array(2) { ["type"]=> string(7) "多边形" ["coordinates"]=> array(1) { [0]=> array(4) { [0]=> array(2) { [0]=> float(139.488892) [1]=> float(35.440878) } [1]=> array(2) { [0]=> float(139.488892) [1]=> float(35.506665) } [2]=> array(2) { [0]=> float(139.570535) [1]=> float(35.506665) } [3]=> array(2) { [0]=> float(139.570535) [1]=> float(35.440878) } } } } ["attributes"]=> array(0) { } } ["contributors"]=> NULL ["is_quote_status"]=> bool(false) ["retweet_count"]=> int(0) ["favorite_count"]=> int(0) ["entities"]=> array(4) { ["hashtags"]=> array(0) { } ["urls"]=> array(0) { } ["user_mentions"]=> array(0) { } ["symbols"]=> array(0) { } } ["favorited"]=> bool(false) ["retweeted"]=> bool(false) ["filter_level"]=> string(3) "low" ["lang"]=> string(2) "ja" ["timestamp_ms"]=> string(13) "1490589667759" }
概括
为 Twitter API 实现 OAuth 身份验证非常麻烦,虽然现在很常见,但过去有很多事情需要自己实现,例如处理访问令牌。不过,令人欣慰的是,现在有一个库可以让你在短短一小时内(而不是五分钟)获取满足你需求的数据,无论你之前遇到多少困难。
就是这样。
0