Posted By

richardmaisano on 06/01/11


Tagged

php simple json twitter


Versions (?)

PHP + JSON + Twitter API


 / Published in: PHP
 

URL: http://www.richardmaisano.com/php-json-twitter-api

Nothing revolutionary, just a simple implementation.

A reworking from Brian Cray's source code: http://briancray.com/2009/08/21/tweeted-links-twitter-api-php-cache/

Note: requires creating a cache file, '/caches/twitter', relative to source.

  1. <?php
  2. // define the parameters you want to use
  3. $username = 'richardmaisano';
  4. $tweet_count = '10';
  5.  
  6. // define the api url
  7. $url = 'https://api.twitter.com/1/statuses/user_timeline.json?screen_name=' . $username . '&count=' . $count . '&include_entities=1&include_rts=1';
  8.  
  9. // define the cache file you created
  10. $cache = dirname(__FILE__) . '/caches/twitter';
  11.  
  12. // check to see if the cache is older than five minutes
  13. // if so, refresh the data
  14. $cache_time = filemtime($cache);
  15. $five_minutes = time() - 300;
  16.  
  17. if ($cache_time > $five_minutes)
  18. {
  19. $data = file_get_contents($cache);
  20. }
  21. else
  22. {
  23. $data = file_get_contents($url);
  24. $cachefile = fopen($cache, 'wb');
  25. fwrite($cachefile, $data);
  26. fclose($cachefile);
  27. }
  28.  
  29. // set up the json return
  30. $json = json_decode($data);
  31.  
  32. // the rest of the script just parses through the data,
  33. // namely the date and time of tweet and any 'entities'
  34. if (!empty($json)) :
  35. foreach ($json as $tweet) :
  36. $datetime = $tweet->created_at;
  37. $date = date('M d, Y', strtotime($datetime));
  38. $time = date('g:ia', strtotime($datetime));
  39. $tweet_text = $tweet->text;
  40.  
  41. // check if any entites exist and if so, replace then with hyperlinked versions
  42. if (!empty($tweet->entities->urls) || !empty($tweet->entities->hashtags) || !empty($tweet->entities->user_mentions)) {
  43. foreach ($tweet->entities->urls as $url) {
  44. $find = $url->url;
  45. $replace = '<a href="'.$find.'">'.$find.'</a>';
  46. $tweet_text = str_replace($find,$replace,$tweet_text);
  47. }
  48.  
  49. foreach ($tweet->entities->hashtags as $hashtag) {
  50. $find = '#'.$hashtag->text;
  51. $replace = '<a href="http://twitter.com/#!/search/%23'.$hashtag->text.'">'.$find.'</a>';
  52. $tweet_text = str_replace($find,$replace,$tweet_text);
  53. }
  54.  
  55. foreach ($tweet->entities->user_mentions as $user_mention) {
  56. $find = "@".$user_mention->screen_name;
  57. $replace = '<a href="http://twitter.com/'.$user_mention->screen_name.'">'.$find.'</a>';
  58. $tweet_text = str_ireplace($find,$replace,$tweet_text);
  59. }
  60. }
  61. ?>
  62.  
  63. <div class="entry tweet">
  64. <span><?php echo $tweet_text; ?></span>
  65. <div class="meta">
  66. <small><?php echo $time; ?>, <?php echo $date; ?></small>
  67. </div>
  68. </div>
  69.  
  70. <?php endforeach; ?>
  71. <?php endif; ?>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: rahuldudeja on February 22, 2013

can we fetch the user profile image also?

You need to login to post a comment.