Guest User

Untitled

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