Skip to content

Instantly share code, notes, and snippets.

@richcaudle
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richcaudle/9bfc3689aca2e8e02c5a to your computer and use it in GitHub Desktop.
Save richcaudle/9bfc3689aca2e8e02c5a to your computer and use it in GitHub Desktop.
<?php
// Include the library
require 'vendor/datasift/datasift-php/lib/datasift.php';
// Create a client
$client = new DataSift_User('YOUR_USERNAME', 'YOUR_APIKEY');
// Declare a filter in CSDL, looking for content mentioning brands, tagging each mentioned
$csdl = 'tag "Calvin Klein" { interaction.content contains "Calvin Klein" }
tag "GQ" { interaction.content contains "GQ" }
tag "Adidas" { interaction.content contains "Adidas" }
return
{
interaction.content contains_any "Calvin Klein, GQ, Adidas"
}';
$filter = $client->createDefinition($csdl);
echo "Filter hash: " . $filter->getHash().PHP_EOL;
// Declare event handling object for stream consumption
class EventHandler implements DataSift_IStreamConsumerEventHandler
{
public function onInteraction($consumer, $interaction, $hash)
{
echo "INTERACTION: ".json_encode($interaction).PHP_EOL.PHP_EOL;
}
// Triggered when a connection is successfully setup
public function onConnect($consumer)
{
echo "Connected to DataSift".PHP_EOL;
}
// Triggered when an error message is received from DataSift
public function onError($consumer, $message)
{
echo 'ERROR: '.$message.PHP_EOL;
}
// Triggered when an interaction is marked as deleted. For sources such as Twitter you must delete this interaction in your application to meet Ts&Cs.
public function onDeleted($consumer, $interaction, $hash)
{
echo 'DELETE: Interaction '.$interaction['interaction']['id'].PHP_EOL;
}
// Ignore other events for quickstart example
public function onStatus($consumer, $type, $info) {}
public function onWarning($consumer, $message) {}
public function onDisconnect($consumer) {}
public function onStopped($consumer, $reason) {}
}
// Get an HTTP stream consumer for the filter, providing the event handler
$consumer = $filter->getConsumer(DataSift_StreamConsumer::TYPE_HTTP, new EventHandler());
// Consume the stream - this will not return unless the stream gets disconnected
$consumer->consume();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment