Guest User

Untitled

a guest
Jan 21st, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * This example is written in PHP
  5. * It demonstrates
  6. * - creating a CSDL statement
  7. * - making an API request to Datasift
  8. * - displaying the data returned
  9. */
  10.  
  11. //Step 1 Define authentication details
  12. define('USERNAME', 'genesis');
  13. define('API_KEY', '79878d08b0d59ac30eab9e1ca36c1e0f');
  14.  
  15. //Step 2 - Include the DataSift library - This is the only file "required"*
  16. require dirname(__FILE__) . '/lib/datasift.php';
  17.  
  18. //Step 3 - Create a "User" object for authentication
  19. $user = new DataSift_User(USERNAME, API_KEY);
  20.  
  21. /**
  22. * Step 4 - Define a CSDL filter for compilation.
  23. * The 'words' array is a set of terms we will filter from Twitter
  24. * Notice the 'interaction.type=="twitter"'
  25. * The use of implode simply concatenates each word in the words array with
  26. * a prefix that results in a string such as:
  27. * interaction.type == "twitter" and (interaction.content contains "music" or interaction.content contains "mtv"
  28. * or interaction.content contains "itv" or interaction.content contains "skyb2b" or interaction.content contains
  29. * "news" or interaction.content contains "csi" or interaction.content contains "criminal minds")
  30. */
  31.  
  32. $words = array('music', 'mtv', 'itv', 'skyb2b', 'news', 'csi', 'criminal minds');
  33.  
  34. // Create the definition
  35. $csdl = 'interaction.type == "twitter" and (interaction.content contains "' . implode('" or interaction.content contains "', $words) . '")';
  36.  
  37. //Step 5 - Create a definition using the user object and the generated CSDL
  38. $definition = new DataSift_Definition($user, $csdl);
  39.  
  40. //some vars to use later
  41. $hash = null;
  42. $created = null;
  43. $cost = null;
  44.  
  45. //Step 6 - explicitly compile the definition
  46. try {
  47. $definition->compile();
  48.  
  49. //Step 7 - If everything is okay then we can now get the hash,created_at and cost values that are returned
  50. $hash = $definition->getHash();
  51. $created = $definition->getCreatedAt();
  52. $cost = $definition->getTotalCost();
  53.  
  54. print 'Stream hash : ' . $hash . " \n";
  55. print 'Created at : ' . $created . " \n";
  56. print 'Total costs : ' . $cost . " \n";
  57. }
  58.  
  59. catch (Exception $e) {
  60.  
  61. //If there is an exception then a few things could have gone wrong so the message included would help
  62. echo 'Caught exception: ', $e->getMessage(), "\n";
  63. }
  64.  
  65. ?>
Add Comment
Please, Sign In to add comment