Returns a DataSift_StreamConsumer-derived object for this definition,
for the given type.
public getConsumer ( string $type, DataSift_IStreamConsumerEventHandler $eventHandler ) : DataSift_StreamConsumer | ||
$type | string | The consumer type for which to construct a consumer. |
$eventHandler | DataSift_IStreamConsumerEventHandler | An instance of DataSift_IStreamConsumerEventHandler |
return | DataSift_StreamConsumer | The consumer object. |
public function consume($filter) { // Save the filter $this->filter = $filter; $definition = new \DataSift_Definition(new \DataSift_User(DATASIFT_USERNAME, DATASIFT_API_KEY), $filter->csdl); // Create the consumer $consumer = $definition->getConsumer(\DataSift_StreamConsumer::TYPE_HTTP, $this); // And start consuming (this will block) $consumer->consume(); }
public function onDisconnect($consumer) { echo 'Disconnected' . PHP_EOL; } /** * Called when the consumer stops for some reason. * * @param DataSift_StreamConsumer $consumer The consumer sending the event. * @param string $reason The reason the consumer stopped. * * @return void */ public function onStopped($consumer, $reason) { echo PHP_EOL . 'Stopped: ' . $reason . PHP_EOL . PHP_EOL; } } // Authenticate echo "Creating user...\n"; $user = new DataSift_User(USERNAME, API_KEY); // Create the definition $csdl = 'interaction.content contains "football"'; echo "Creating definition...\n {$csdl}\n"; $definition = new DataSift_Definition($user, $csdl); // Create the consumer echo "Getting the consumer...\n"; $consumer = $definition->getConsumer(DataSift_StreamConsumer::TYPE_HTTP, new EventHandler()); // And start consuming echo "Consuming...\n--\n"; $consumer->consume(); echo "Finished consuming\n\n";
public function testGetConsumer() { $response = array('response_code' => 200, 'data' => array('hash' => testdata('definition_hash'), 'created_at' => date('Y-m-d H:i:s', time()), 'dpu' => 10), 'rate_limit' => 200, 'rate_limit_remaining' => 150); DataSift_MockApiClient::setResponse($response); $def = new DataSift_Definition($this->user, testdata('definition')); $this->assertEquals($def->get(), testdata('definition')); $consumer = $def->getConsumer(DataSift_StreamConsumer::TYPE_HTTP, new DummyEventHandler()); $this->assertInstanceOf('DataSift_StreamConsumer', $consumer, 'Failed to get an HTTP stream consumer object'); }