# dev-php-getlastweet
PHP (plain)
  1. // API LIB https://github.com/J7mbo/twitter-api-php
  2.  
  3. function get_last_tweet()
  4. {
  5. /** Set access tokens here - see: https://dev.twitter.com/apps/ **/
  6. $settings = array(
  7. 'oauth_access_token' => "XXX",
  8. 'oauth_access_token_secret' => "XXX",
  9. 'consumer_key' => "XXX",
  10. 'consumer_secret' => "XXX"
  11. );
  12. /** Perform a GET request and echo the response **/
  13. /** Note: Set the GET field BEFORE calling buildOauth(); **/
  14. $url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
  15. $getfield = '?screen_name=USERNAME&count=1';
  16. $requestMethod = 'GET';
  17. $twitter = new TwitterAPIExchange($settings);
  18. $json = $twitter->setGetfield($getfield)->buildOauth($url, $requestMethod)->performRequest();
  19. $json = json_decode($json);
  20.  
  21. // the rest of the script just parses through the data,
  22. // namely the date and time of tweet and any 'entities'
  23. // Borrowed from http://snipplr.com/view/54710/
  24.  
  25. if (!empty($json)):
  26. foreach($json as $tweet):
  27. $datetime = $tweet->created_at;
  28. $date = date('M d, Y', strtotime($datetime));
  29. $time = date('g:ia', strtotime($datetime));
  30. $tweet_text = $tweet->text;
  31.  
  32. // check if any entities exist and if so, replace then with hyperlinked versions
  33.  
  34. if (!empty($tweet->entities->urls) || !empty($tweet->entities->hashtags) || !empty($tweet->entities->user_mentions))
  35. {
  36. foreach($tweet->entities->urls as $url)
  37. {
  38. $find = $url->url;
  39. $replace = '<a href="' . $find . '">' . $find . '</a>';
  40. $tweet_text = str_replace($find, $replace, $tweet_text);
  41. }
  42.  
  43. foreach($tweet->entities->hashtags as $hashtag)
  44. {
  45. $find = '#' . $hashtag->text;
  46. $replace = '<a href="http://twitter.com/#!/search/%23' . $hashtag->text . '">' . $find . '</a>';
  47. $tweet_text = str_replace($find, $replace, $tweet_text);
  48. }
  49.  
  50. foreach($tweet->entities->user_mentions as $user_mention)
  51. {
  52. $find = "@" . $user_mention->screen_name;
  53. $replace = '<a href="http://twitter.com/' . $user_mention->screen_name . '">' . $find . '</a>';
  54. $tweet_text = str_ireplace($find, $replace, $tweet_text);
  55. }
  56. }
  57.  
  58. endforeach;
  59. endif;
  60. return $tweet_text;
  61. }
  62.  
  63. echo get_last_tweet();

The main thing is not death, but what dies inside us while we live
Last Changed: 2025/10/27 19:59
Made with pure php. All information on this website is only for educational purposes.