public function testConsumeShouldExecuteGivenCallback() { $eventDispatcher = new \Armadillo\EventDispatcher(); $consumer = new Consumer($this->queue, $eventDispatcher); $callbackWasExecuted = false; $eventDispatcherClass = ""; $consumeCallback = function ($message, $eventDispatcher) use(&$callbackWasExecuted, &$eventDispatcherClass) { $callbackWasExecuted = true; $eventDispatcherClass = get_class($eventDispatcher); }; $consumer->consume($consumeCallback); $this->assertTrue($callbackWasExecuted); $this->assertEquals("Armadillo\\EventDispatcher", $eventDispatcherClass); }
$this->oDB = new db(); } // 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) { $tweet_object = json_decode($status); // Ignore tweets without a properly formed tweet id value if (!isset($tweet_object->id_str)) { return; } $tweet_id = $tweet_object->id_str; // If there's a ", ', :, or ; in object elements, serialize() gets corrupted // You should also use base64_encode() before saving this $raw_tweet = base64_encode(serialize($tweet_object)); $field_values = 'raw_tweet = "' . $raw_tweet . '", ' . 'tweet_id = ' . $tweet_id; $this->oDB->insert('json_cache', $field_values); } } // Open a persistent connection to the Twitter streaming API $stream = new Consumer(OAUTH_TOKEN, OAUTH_SECRET, Phirehose::METHOD_FILTER); // Establish a MySQL database connection $stream->db_connect(); // The keywords for tweet collection are entered here as an array // More keywords can be added as array elements // For example: array('recipe','food','cook','restaurant','great meal') $stream->setTrack(array('#ris_zh', 'tympaan', '#tympaan', '@tympaan_inst', '@Tympaan_inst', 'Tympaan', '#databankzh')); // Start collecting tweets // Automatically call enqueueStatus($status) with each tweet's JSON data $stream->consume();
<?php require_once '../../config/config.php'; require_once '../../lib/Thumper/Consumer.php'; require_once '../../lib/php-amqplib/amqp.inc'; $myConsumer = function ($msg) { echo $msg, "\n"; }; $consumer = new Consumer(HOST, PORT, USER, PASS, VHOST); $consumer->setExchangeOptions(array('name' => 'hello-exchange', 'type' => 'direct')); $consumer->setQueueOptions(array('name' => 'hello-queue')); $consumer->setCallback($myConsumer); //myConsumer could be any valid PHP callback $consumer->consume(5); //5 is the number of messages to consume
echo 'Message received, processing for 10 seconds... '; // dodgy wait loop - sleep is interrupted by signals $endTime = microtime(true) + 10; while (microtime(true) < $endTime) { sleep(1); } echo "Done\n"; $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']); // also handle signals between messages pcntl_signal_dispatch(); } public function consume() { while (count($this->ch->callbacks)) { $this->ch->wait(); } } public function handleSignal($sig) { echo "\nCaught signal " . $sig . "\n"; if (SIGINT == $sig) { $this->ch->basic_cancel($this->queue); $this->ch->close(); $this->conn->close(); exit; } } } $consumer = new Consumer(); $consumer->consume();
<?php require "vendor/autoload.php"; require "lib/Consumer.php"; use Fennb\Phirehose\OauthPhirehose; $c = new Consumer('447957969-kz0OCtJcgRd7S5PYkT2rTxIV5sTxlT5wXku9InH4', 'RMyT6utD7tpdtvAy7RqFFLUuL3bUDLBVXO5Rwr05WcWQz', Phirehose::METHOD_FILTER); $c->db = new mysqli("localhost", "tweets_user", "lovepotion", "tweets"); $c->setLocations([[-74.5, 40, -73.5, 41]]); $c->consume();