Posted By

tomdowning on 09/22/11


Tagged

twitter


Versions (?)

PHP Twitter feed


 / Published in: PHP
 

  1. function fetch_twitter_feed($since = 0) {
  2. $url = 'http://twitter.com/statuses/user_timeline.xml?count=3&screen_name=twittername';
  3. if($since > 0) {
  4. $url .= '&since_id=' . $since;
  5. }
  6. $data = @file_get_contents($url);
  7. if($data == false) return 'Tweets unavailable';
  8.  
  9. $xmlDoc = DOMDocument::loadXML($data);
  10. $statuses = $xmlDoc->getElementsByTagName('status');
  11. $updates = array();
  12.  
  13. if($statuses->length > 0) {
  14. foreach($statuses as $status) {
  15. $id = $status->getElementsByTagName('id')->item(0)->nodeValue;
  16. $text = $status->getElementsByTagName('text')->item(0)->nodeValue;
  17. $date = strtotime($status->getElementsByTagName('created_at')->item(0)->nodeValue);
  18. $updates[] = array($id, $text, $date);
  19. } }
  20.  
  21. return $updates;
  22. }
  23.  
  24. $updates = fetch_twitter_feed();
  25. if(empty($updates)) { ?>
  26. <p style="font-size:10px;">No Tweets Yet!</p>
  27. <? } else if($updates == 'Twitter unavailable') { ?>
  28. <p style="font-size:10px;>Tweets unavailable</p>
  29. <? } else {
  30. foreach($updates as $update) {
  31. $text = substr($update[1], 0,50);
  32. if(strlen($text) == 50) $text .= '...';
  33. $date = date('d.m.y', $update[2]); ?>
  34.  
  35. <p style="font-size:10px;"><a href="http://twitter.com/twittername/status/<? echo $update[0]; ?>" target="_blank"><?php echo $text; ?></a></p>
  36. <?php }
  37. } ?>

Report this snippet  

You need to login to post a comment.