简体   繁体   English

如何自定义和使用Phirehose功能?

[英]How do I customize and use Phirehose functions?

I'm trying to put in a check for Phirehose to stop running after 10 seconds or 100 tweets...basically, I want to be able to stop the script. 我正在尝试检查Phirehose在10秒或100条鸣叫后是否停止运行...基本上,我希望能够停止脚本。

I was told I could customize the statusUpdate() function or the heartBeat() function, but I'm uncertain how to do that. 有人告诉我可以自定义statusUpdate()函数或heartBeat()函数,但是我不确定该怎么做。 Right now, I'm just testing with the filter-track.php example. 现在,我只是用filter-track.php示例进行测试。

How do I customize the functions, and where should I call them in the class? 如何自定义函数,在类中应该在哪里调用?

class FilterTrackConsumer extends OauthPhirehose
{
  /**
   * Enqueue each status
   *
   * @param string $status
   */

  public function enqueueStatus($status)
  {

    /*
     * In this simple example, we will just display to STDOUT rather than enqueue.
     * NOTE: You should NOT be processing tweets at this point in a real application, instead they should be being
     *       enqueued and processed asyncronously from the collection process.
     */
    $data = json_decode($status, true);
    if (is_array($data) && isset($data['user']['screen_name'])) {
      print $data['user']['screen_name'] . ': ' . urldecode($data['text']) . "\n";
    }


  }

  public function statusUpdate()
  {
    return "asdf";
  }

}

// The OAuth credentials you received when registering your app at Twitter
define("TWITTER_CONSUMER_KEY", "");
define("TWITTER_CONSUMER_SECRET", "");


// The OAuth data for the twitter account
define("OAUTH_TOKEN", "");
define("OAUTH_SECRET", "");

// Start streaming
$sc = new FilterTrackConsumer(OAUTH_TOKEN, OAUTH_SECRET, Phirehose::METHOD_FILTER);
$sc->setLang('en');
$sc->setTrack(array('love'));
$sc->consume();

To stop after 100 tweets, have a counter in that function receiving the tweets, and call exit when done: 要在100条推文后停止,请在该函数中让一个计数器接收推文,并在完成后调用exit:

class FilterTrackConsumer extends OauthPhirehose
{
  private $tweetCount = 0; 
  public function enqueueStatus($status)
  {
    //Process $status here
    if(++$this->tweetCount >= 100)exit;
  }
...

(Instead of exit you could throw an exception, and put a try/catch around your $sc->consume(); line.) (代替exit您可以抛出异常,并在$sc->consume();行周围进行try / catch。)

For shutdown "after 10 seconds", this is easy if it can be roughly 10 seconds (ie put a time check in enqueueStatus() , and exit if it has been more than 10 seconds since the program started), but hard if you want it to be exactly 10 seconds. 对于“ 10秒后”关机,如果大约10秒钟很容易(例如,将时间检查放在enqueueStatus() ,如果自程序启动以来已经超过10秒钟,则退出),但是如果需要,则很难恰好是10秒。 This is because enqueueStatus() is only called when a tweet comes in. So, as an extreme example, if you get 200 tweets in the first 9 seconds, but then it goes quiet and the 201st tweet does not arrive for 80 more seconds, your program would not exit until the program has been running 89 seconds. 这是因为enqueueStatus()仅在一条tweet enqueueStatus()时才被调用。因此,举一个极端的例子,如果在前9秒内收到200条tweet,但随后它变得安静,而第201条tweet在80秒后再没有到达,您的程序只有在运行89秒后才会退出。

Taking a step back, wanting to stop Phirehose is normally a sign it is the wrong tool for the job. 退后一步,想要停止Phirehose通常是一个信号,它是这项工作的错误工具。 If you just want to poll 100 recent tweets, every now and again, then the REST API, doing a simple search, is better. 如果您只想一次又一次地轮询100条最近的推文,那么进行简单搜索的REST API会更好。 The streaming API is more for applications that intend to run 24/7, and want to react to tweets as soon as they are, well, tweeted. 流API更适合打算运行24/7并希望在发推时立即对推做出反应的应用程序。 (More critically, Twitter will rate-limit, or close, your account if you connect too frequently.) (更重要的是,如果连接频率过高,Twitter将对您的帐户进行速率限制或关闭。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM