×

Welcome to TagMyCode

Please login or create account to add a snippet.
1
0
 
0
Language: PHP
Posted by: Bob Ray
Added: Jul 16, 2015 10:28 PM
Modified: Jul 16, 2015 10:34 PM
Views: 8
Tags: tweets hashtag
Gets all tweets with a given hashtag
  1. /* Original code by Mohit Madan @TheMohitMadan */
  2.  
  3. function getTweets($hash_tag) {
  4.  
  5.     $url = 'http://search.twitter.com/search.atom?q='.urlencode($hash_tag) ;
  6.     echo "<p>Connecting to <strong>$url</strong> ...</p>";
  7.     $ch = curl_init($url);
  8.     curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE);
  9.     $xml = curl_exec ($ch);
  10.     curl_close ($ch);
  11.  
  12.     //If you want to see the response from Twitter, uncomment this next part out:
  13.     //echo "<p>Response:</p>";
  14.     //echo "<pre>".htmlspecialchars($xml)."</pre>";
  15.  
  16.     $affected = 0;
  17.     $twelement = new SimpleXMLElement($xml);
  18.     foreach ($twelement->entry as $entry) {
  19.         $text = trim($entry->title);
  20.         $author = trim($entry->author->name);
  21.         $time = strtotime($entry->published);
  22.         $id = $entry->id;
  23.         echo "<p>Tweet from ".$author.": <strong>".$text."</strong>  <em>Posted ".date('n/j/y g:i a',$time)."</em></p>";
  24.     }
  25.  
  26.     return true ;
  27. }