Creates and returns an empty Definition object.
public createDefinition ( string $definition = '' ) : DataSift_Definition | ||
$definition | string | Optional definition with which to prime the object. |
return | DataSift_Definition | A definition object tied to this user. |
// Ignore the other events for the purposes of this example. public function onConnect($consumer) { } public function onDeleted($consumer, $interaction, $hash) { } public function onStatus($consumer, $type, $info) { } public function onWarning($consumer, $message) { } public function onError($consumer, $message) { } public function onDisconnect($consumer) { } public function onStopped($consumer, $reason) { } } // Create the user $user = new DataSift_User('username', 'api_key'); // Create a definition looking for the word "datasift" $def = $user->createDefinition('interaction.content contains "datasift"'); // Get an HTTP stream consumer for that definition $consumer = $def->getConsumer(DataSift_StreamConsumer::TYPE_HTTP, new EventHandler()); // Consume it - this will not return unless the stream gets disconnected $consumer->consume();
/** * Constructor. Do not use this directly, use the factory method instead. * * @param DataSift_User $user The user this consumer will run as. * @param mixed $definition CSDL string, a Definition object, or an array of hashes. * @param DataSift_IStreamConsumerEventHandler $eventHandler The object that will receive events. * * @throws DataSift_Exception_InvalidData * @throws DataSiftExceotion_CompileFailed * @throws DataSift_Exception_APIError */ protected function __construct($user, $definition, $eventHandler) { if (!$user instanceof DataSift_User) { throw new DataSift_Exception_InvalidData('Please supply a valid DataSift_User object when creating a DataSift_StreamConsumer object.'); } if (is_array($definition) && count($definition) > 0) { // Yes, we're multi $this->_is_multi = true; // Get the hashes foreach ($definition as $d) { if ($d instanceof DataSift_Definition) { $this->_hashes[] = $d->getHash(); } else { $this->_hashes[] = $d; } } } elseif (is_string($definition)) { // Convert the CSDL into a Definition object $this->_definition = $user->createDefinition($definition); } elseif ($definition instanceof DataSift_Definition) { // Already a Definition object $this->_definition = $definition; } else { throw new DataSift_Exception_InvalidData('The definition must be a CSDL string, a DataSift_Definition object, or an array of stream hashes.'); } // Set the user $this->_user = $user; // Validate and set the event handler if (!$eventHandler instanceof DataSift_IStreamConsumerEventHandler) { throw new DataSift_Exception_InvalidData('Your event handler object must implement the DataSift_IStreamConsumerEventHandler interface.'); } $this->_eventHandler = $eventHandler; // Ask for the definition hash - this will compile the definition if // necessary if (!$this->_is_multi) { $this->_definition->getHash(); } }
*/ /** * This script creates a pull subscription and requests data from it periodically. * */ // Include the DataSift library require dirname(__FILE__) . '/../../lib/datasift.php'; // Include the configuration - put your username and API key in this file require dirname(__FILE__) . '/../../config.php'; // Authenticate echo "Creating user...\n"; $user = new DataSift_User(USERNAME, API_KEY); //Define some CSDL $csdl = 'interaction.content contains "coffee" AND interaction.type == "tumblr"'; // Create the stream definition $stream_definition = $user->createDefinition($csdl); //Create the push definition $push_definition = $user->createPushDefinition(); $push_definition->setOutputType('pull'); $push_sub = $push_definition->subscribeDefinition($stream_definition, 'My PHP pull subscription'); echo "Pull subscription created, ID: " . $push_sub->getId() . "\n"; //Pull 10 times, every 2 seconds for ($i = 1; $i <= 10; $i++) { sleep(2); echo "Pull number {$i}\n"; $interactions = $push_sub->pull(); foreach ($interactions as $interaction) { if (isset($interaction['interaction']['content'])) { echo "{$interaction['interaction']['content']}\n"; } }