Created
September 20, 2011 11:03
-
-
Save edstenson/1228865 to your computer and use it in GitHub Desktop.
api.datasift.net/compile in PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* This example is written in PHP | |
* It demonstrates | |
* - creating a CSDL statement | |
* - making an API request to Datasift | |
* - displaying the data returned | |
*/ | |
//Step 1 Define authentication details | |
define('USERNAME', 'genesis'); | |
define('API_KEY', '79878d08b0d59ac30eab9e1ca36c1e0f'); | |
//Step 2 - Include the DataSift library - This is the only file "required"* | |
require dirname(__FILE__) . '/lib/datasift.php'; | |
//Step 3 - Create a "User" object for authentication | |
$user = new DataSift_User(USERNAME, API_KEY); | |
/** | |
* Step 4 - Define a CSDL filter for compilation. | |
* The 'words' array is a set of terms we will filter from Twitter | |
* Notice the 'interaction.type=="twitter"' | |
* The use of implode simply concatenates each word in the words array with | |
* a prefix that results in a string such as: | |
* interaction.type == "twitter" and (interaction.content contains "music" or interaction.content contains "mtv" | |
* or interaction.content contains "itv" or interaction.content contains "skyb2b" or interaction.content contains | |
* "news" or interaction.content contains "csi" or interaction.content contains "criminal minds") | |
*/ | |
$words = array('music', 'mtv', 'itv', 'skyb2b', 'news', 'csi', 'criminal minds'); | |
// Create the definition | |
$csdl = 'interaction.type == "twitter" and (interaction.content contains "' . implode('" or interaction.content contains "', $words) . '")'; | |
//Step 5 - Create a definition using the user object and the generated CSDL | |
$definition = new DataSift_Definition($user, $csdl); | |
//some vars to use later | |
$hash = null; | |
$created = null; | |
$cost = null; | |
//Step 6 - explicitly compile the definition | |
try { | |
$definition->compile(); | |
//Step 7 - If everything is okay then we can now get the hash,created_at and cost values that are returned | |
$hash = $definition->getHash(); | |
$created = $definition->getCreatedAt(); | |
$cost = $definition->getTotalCost(); | |
print 'Stream hash : ' . $hash . " \n"; | |
print 'Created at : ' . $created . " \n"; | |
print 'Total costs : ' . $cost . " \n"; | |
} | |
catch (Exception $e) { | |
//If there is an exception then a few things could have gone wrong so the message included would help | |
echo 'Caught exception: ', $e->getMessage(), "\n"; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment