Posted By

LuckyShot on 04/08/10


Tagged

php rss twitter pregreplace webscraping


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

LuckyShot
BrianCoyDesign
dantreacy


Twitter - Print latest tweet from user (extracts from RSS feed in PHP and linkifies links)


 / Published in: PHP
 

Extracts the most recent tweet from the users RSS feed and auto-detects links.

  1. function parse_feed($feed) {
  2. '/<description>(.*?)<\/description>/s',
  3. $feed,
  4. $tweets,
  5. PREG_SET_ORDER
  6. );
  7. $tweet = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","<a href=\"\\0\">\\0</a>", $tweets[1][1]);
  8. return $tweet;
  9. }
  10.  
  11. $feed = "http://twitter.com/statuses/user_timeline/26248137.rss";
  12. $twitterFeed = file_get_contents($feed);
  13. echo parse_feed($twitterFeed);
  14.  
  15.  
  16. // Without the function and storing it in a user's cookie that is checked every 30min
  17. $feed = file_get_contents("http://twitter.com/statuses/user_timeline/26248137.rss");
  18. '/<description>(.*?)<\/description>/s',
  19. $feed,
  20. $tweets,
  21. PREG_SET_ORDER
  22. );
  23. $tweet = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","<a href=\"\\0\">\\0</a>", $tweets[1][1]);
  24.  
  25. if (!$_COOKIE['lasttweet']) {
  26. setcookie("lasttweet", $tweet, time()+1800); /* expire in 30min */
  27. echo $tweet;
  28. }else{
  29. echo $_COOKIE['lasttweet'];
  30. }

Report this snippet  

You need to login to post a comment.