twitter xml feeds are back!

Jun 25

As you may have heard twitter have final fully deprecated version 1 of their API. One of the most annoying things about this is that it takes away all the open feeds and search functionality.....In fact it's particularly annoying for me because only recently I wrote a little article on how to selectively post to Facebook from twitter and suddenly my cunning solution doesn't work anymore.

Well I was not to be defeated, and anyway I needed to get my head around using the twitter API v1.1 so I spent a bit of time this afternoon doing a basic app to convert the JSON feed (which does still exist) to RSS. I should quantify at this point that I'm pretty sure doing this and leaving it open or using it to post to Facebook does contravene the twitter terms, specifically point 5 on this summary page, and this was really a training exercise which I thought I'd share as it took me a bit of time not particularly something I plan to leave in place.

One of the major differences with the 1.1 version of the API is that you pretty much need to authenticate with OAuth for everything you're doing. The good news here is that twitter have provided a bunch of OAuth libraries for you to use though as ever if you want to do this with classic ASP you're gonna have a right old ball-ache! Anyway you'll also need to get yourself a bunch of tokens and keys for this which is done via the control panel and explained here (you've no idea how long I took to find this!).

Once you've got your library and your various keys and tokens the rest is pretty straight forward. So here is the PHP script (minus my tokens) that I wrote to convert the response from twitter to an RSS feed including images as enclosures (which even the version 1 API didn't do!!).

  1. // Include the twitter oauth library for PHP (I put it in a subdirectory
  2. require_once("twitteroauth/twitteroauth.php");
  4. // Init
  5. define("CONSUMER_KEY", "[insert your key here]");
  6. define("CONSUMER_SECRET", "[insert your secret here]");
  7. define("OAUTH_TOKEN", "[insert your token here]");
  8. define("OAUTH_TOKEN_SECRET", "[insert your token secret here]");
  10. // Get an authorised connection to twitter
  12. $content = $connection->get("search/tweets.json?q=%23fb%20from%3Atappetyclick&result_type=popular");
  14. // Send the xml header
  15. header("Content-Type: application/rss+xml; charset=ISO-8859-1");
  16. $url = 'http://'.$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
  17. $xml.= '{C}{C}{C}{C}<!--?xml version="1.0" encoding="ISO-8859-1"?-->'.PHP_EOL;
  18. $xml.= '<rss version="2.0" xmlns:atom="">'.PHP_EOL;
  19. $xml.= ' <channel>'.PHP_EOL;
  20. $xml.= '<title></title>'.PHP_EOL;
  21. $xml.= ' <description>Search of the tappetyclick timeline using #fb hashtag search</description>'.PHP_EOL;
  22. $xml.= '<link>'.$url.''.PHP_EOL;
  23. $xml.= ' <atom:link href="'.$url.'" rel="self" type="application/rss+xml">'.PHP_EOL;
  25. // Get the status
  26. foreach ($content->statuses as $status) {
  27. // Build the tweet url as we don't get this in the status object
  28. $url = ''.$status->user->screen_name.'/status/'.$status->id_str;
  29. $date = explode(' ', $status->created_at);
  30. $xml.= ' <item>'.PHP_EOL;
  31. $xml.= '<title></title>'.PHP_EOL;
  32. $xml.= ' <description>'.$status->text.'</description>'.PHP_EOL;
  33. $xml.= '<link>'.$url.''.PHP_EOL;
  34. $xml.= ' <guid ispermalink="true">'.$url.'</guid>'.PHP_EOL;
  35. // Format the date since the twitter format doesn't work for rss
  36. $xml.= ' <pubdate>'.$date[0].', '.$date[2].' '.$date[1].' '.$date[5].' '.$date[3].' '.$date[4].'</pubdate>'.PHP_EOL;
  37. // Get the attached media
  38. if ($status->entities) {
  39. if (is_array($status->entities->media)) {
  40. foreach ($status->entities->media as $media) {
  41. switch ($media->type) {
  42. //Currently only photo's supported but I suspected with vine video will be along soon
  43. case 'photo':
  44. $enc_type = 'image/jpeg';
  45. break;
  46. }
  47. if (!empty($enc_type)) {
  48. // We need the file size for the media so try to get this from the headers
  49. $headers = get_headers($media->media_url);
  50. $size = $headers['Content-Length'];
  51. if (empty($size)) {
  52. foreach ($headers as $header) {
  53. $h = explode(':', $header);
  54. if ($h[0] == 'Content-Length') {
  55. $size = trim($h[1]);
  56. break; // Found what we need, stop looping
  57. }
  58. }
  59. }
  60. if (empty($size)) {
  61. $size = 1; //This is basically a hack to make the rss validate
  62. }
  63. $xml.= ' <enclosure length="'.$size.'" type="'.$enc_type.'" url="'.$media->media_url.'">'.PHP_EOL;
  64. }
  65. }
  66. }
  67. }
  68. $xml.= ' </enclosure></item>'.PHP_EOL;
  69. }
  70. $xml.= ' </atom:link></channel>'.PHP_EOL;
  71. $xml.= '</rss>'.PHP_EOL;
  73. // Return the xml for the rss
  74. print $xml;
  75. ?>

And now should you want to be really naughty you can once again post to Facebook from twitter using RSS graphitti


Recent Comments

Browse by date

Join us on Facebook