richweber/yii2-phirehose

Twitter流式API的Yii2扩展

安装: 257

依赖关系: 0

建议者: 0

安全: 0

星标: 3

关注者: 2

分支: 189

类型:yii2-extension

v1.0.1 2014-09-30 23:30 UTC

This package is auto-updated.

Last update: 2024-09-13 10:18:13 UTC


README

PHP接口,用于Twitter流式API(firehose等)。这个库使得通过流式API连接和消费Twitter流变得容易。

安装

安装此扩展的首选方式是通过 composer

运行以下命令之一:

$ php composer.phar require richweber/yii2-phirehose "dev-master"

或者添加以下内容到你的 composer.json 文件的 require 部分:

"richweber/yii2-phirehose": "dev-master"

使用

组件配置

'components' => [
    ...
    'stream' => [
        'class' => 'richweber\twitter\streaming\lib\Stream',
        'username' => '1111111111-pPylwxC33VekLORMEfBIqYq8qekK4SqiD8pQpTs',
        'password' => 'OpKLltXkXBIUd4RQLwY8slLg3iIo2BCpXlgvkqxEBn33X',
        'consumerKey' => 'rvyKdnYDN887ohIfQU8m7tnxy',
        'consumerSecret' => 'oCYPFJSRPVlelJhEUCGVE8Aps0s4GpEfvNFR1ESQ01xTcq0xYL',
        'method' => 'user',
        'format' => 'json',
    ],
    ...
],

构建Yii控制台命令

<?php

namespace console\controllers;

use richweber\twitter\streaming\lib\Phirehose;

class StreamController extends \yii\console\Controller
{
    public function actionIndex()
    {
        Yii::$app->stream->consume();
    }
}

激活扩展

你可以使用PHP控制台命令测试Phirehose

php yii stream

Twitter将为用户账户发送一系列关注者信息流,然后实时发送到达的数据。

为了将Phirehose激活为一个始终开启的守护进程控制台命令,我们将使用 nohup命令,例如no hangup,并将输出重定向到dev/null

nohup php yii stream > /dev/null 2>&1&

Ubuntu将响应你的进程ID,用于未来的监控和终止

[1] 1376

如果你想检查进程是否在运行,扫描任务列表以获取作业ID

ps -e all | grep 1376

你应该会看到类似这样的内容

0 1000 1389 20855 20 0 17436 928 pipe_w S+ pts/8 0:00 grep --color=auto 1376 nohup php yii stream > /dev/null 2>&1

你可以通过终止作业ID来终止Phirehose

kill 1376

记录从Twitter接收到的数据

我们希望最小化实时响应所需的处理量。本质上,我们只想将Twitter接收到的数据记录到我们的数据库中——仅此而已。我们可以在自己的后台任务中进行其他处理,而不必减慢Phirehose的流连接速度。我的示例只是从传入的流中提取推文数据并将其存储在流表中

<?php

namespace common\components;

use richweber\twitter\streaming\lib\Stream;
use common\models\Twitts;

class MyStream extends Stream
{
    // This function is called automatically by the Phirehose class
    // when a new tweet is received with the JSON data in $status
    public function enqueueStatus($status)
    {
        $stream = json_decode($status);
        if (!(isset($stream->id_str))) { return; }

        $model = new Twitts;
        $model->tweet_id = $stream->id_str;
        $model->code = base64_encode(serialize($stream));
        $model->is_processed = 0;
        $model->created_at = time();
        $model->save();

        var_dump($stream);
    }
}

别忘了在配置文件中更改类。

'components' => [
    ...
    'stream' => [
        'class' => 'common\components\MyStream',
        ...
    ],
    ...
],

Phirehose

参见

目标

  • 为PHP应用程序提供Twitter流式API的简单接口
  • 遵守流式API错误处理、重新连接等建议
  • 鼓励良好的流式API客户端
  • 独立于PHP扩展操作(例如:共享内存、PCNTL等)

此库执行的操作

  • 处理连接/认证到Twitter流式API
  • 消费流,并将每个状态传递给你选择的任何方法进行排队
  • 在连接和API错误上处理优雅的重新连接/回退
  • 监控/报告性能指标和错误

此库不执行的操作

  • 解码/处理推文
  • 提供任何类型的队列机制以进行异步处理(尽管提供了一些示例)
  • 提供任何类型的进程间通信
  • 提供任何非流式API功能(例如:用户资料信息、搜索等)

如何使用

请参阅示例子目录以获取示例用法。在每个示例文件中,你需要插入自己的oauth token/secret以及你创建的Twitter应用的key/secret。

  • filter-oauth.php展示了如何关注特定关键词。
  • sample.php展示了如何获取所有公开状态的随机小样本。
  • userstream-alternative.php展示了如何获取用户流。(一个用户的全部活动。)
  • sitestream.php 展示了如何获取站点流。(多个用户的所有活动。)

请参阅[Wiki](https://github.com/fennb/phirehose/wiki/Introduction)以获取文档。

如果您有任何其他问题,请访问 Phirehose 用户组[http://groups.google.com/group/phirehose-users](http://groups.google.com/group/phirehose-users)。

如果您正在积极使用 Phirehose,建议您加入(或至少定期查看)此组,这样我可以在发布新版本时通知您。

此外,如果您想直接联系我,请在twitter上关注@fennb