Displaying twitter feeds using PHP


SUBMITTED BY: Guest

DATE: May 3, 2013, 9:15 a.m.

FORMAT: PHP

SIZE: 2.7 kB

HITS: 1664

  1. <?php
  2. $twitter_id = 'espncricinfo';
  3. $number_of_tweets = 4;
  4. function linkable($text = ''){
  5. $text = preg_replace("/\s+/", ' ', str_replace(array("\r\n", "\r", "\n"), ' ', $text));
  6. $data = '';
  7. foreach( explode(' ', $text) as $str){
  8. if (preg_match('#^http?#i', trim($str)) || preg_match('#^www.?#i', trim($str))) {
  9. $data .= '<a href="'.$str.'">'.$str.'</a> ';
  10. } else {
  11. $data .= $str .' ';
  12. }
  13. }
  14. return trim($data);
  15. }
  16. ?>
  17. <div style="margin: auto; width:350px; border: 10px solid rgb(177, 212, 252); border-radius: 10px 10px 10px 10px;" id="twitter_lf">
  18. <div class="twitterSearchTitle" style="background-color: rgb(177, 212, 252); margin: 0px; padding: 0px 0px 5px; text-align: center; font-weight: bold; font-size: large; position: relative;">
  19. <a href="http://twitter.com/abiosystems" style="text-decoration: none; color: rgb(59, 89, 152);">
  20. @<?php echo $twitter_id;?> <br /> <span style="font-size:10px;">Live Twitter Feed</span></a><a href="http://www.twitter.com">
  21. <img src="http://cloud.github.com/downloads/malsup/twitter/tweet.gif" class="twitterSearchBird" style="width: 50px; height: 20px; position: absolute; left: -30px; top: -20px; border: medium none;">
  22. </a>
  23. </div>
  24. <div class="twitterSearchContainter" style="overflow: hidden; background-color: rgb(255, 255, 255); height: auto; display: block;">
  25. <?php
  26. //by dharam to get twitter feed
  27. $responseJson = file_get_contents('http://api.twitter.com/1/statuses/user_timeline.json?screen_name='.$twitter_id.'&include_rts=1&count='.$number_of_tweets);
  28. if($responseJson) {
  29. $responseAray = json_decode($responseJson);
  30. foreach($responseAray as $resp) {
  31. ?>
  32. <div style="padding:3px; margin:0 0 2px 0;">
  33. <a style="color:#00BEF2;" href="https://twitter.com/intent/user?screen_name=abiosystems" target="_blank">@<?php echo $resp->user->screen_name;?></a> <?php echo linkable($resp->text);?><br />
  34. <a style="color:#0E313B;" href="https://twitter.com/intent/tweet?in_reply_to=<?php echo $resp->id_str;?>">reply</a> |
  35. <a style="color:#0E313B;" href="https://twitter.com/intent/retweet?tweet_id=<?php echo $resp->id_str;?>">retweet</a> |
  36. <a style="color:#0E313B;" href="https://twitter.com/intent/favorite?tweet_id=<?php echo $resp->id_str;?>">favorite</a>
  37. </div>
  38. <?php
  39. }
  40. }
  41. ?>
  42. </div>
  43. </div>

comments powered by Disqus