Monday, 15 October 2012

Twitter changes their format for status feeds

Tweets not showing up? Twitter changes their feed format for status feeds

You may have noticed over the couple of weeks that the usual Tweets from my Twitter account @strictlytweets have not been showing in the right hand side bar.

This is because Twitter have changed the URL that you must request to get those tweets from.

http://twitter.com/statuses/user_timeline/strictlytweets.json?count=10

To their new API which uses a secure URL (e.g from http to https)

https://api.twitter.com/1/statuses/user_timeline.json?screen_name=strictlytweets&count=10


Now if you have a callback function it would be called in the same way with a parameter passed in the URL for the name of the function to be called.

Although I am not sure whether Twitter have changed their name of their standard function which formats Tweets into links from twitterCallback to twitterCallback2 if you take a look at the one blogger uses you can see they are calling it twitterCallback2.

You can see their script here:

http://twitter.com/javascripts/blogger.js

Therefore if you have a special function that you need to call to format your tweets in your own style, cache on your own server or scroll them you will still need to add the name of your callback function to the script source and make sure it accepts a JSON hash table of Tweet objects.

You will also need to reference this script before calling the new script URL that gets the statuses.

If you look at this blog you can see that the first script loads in bloggers own formatting of the Tweets in  through a call to blogger.js and the second one gets the new Twitter feed status format in the new URL format using JSON.


<script type="text/javascript" src="http://twitter.com/javascripts/blogger.js"></script>
<script type="text/javascript" src="https://api.twitter.com/1/statuses/user_timeline.json?screen_name=strictlytweets&callback=twitterCallback2&count=6"></script>


Twitter are constantly changing and like most sites are moving to secure URL's e.g https and using JSON instead of XML/RSS to return data to the user.

So if you have a Twitter account and want to show your own Tweets on your site and it hasn't been working lately this is the reason why.

Just switch over to the new https format and you will be okay as long as you also implement some form of caching to handle the rate limit which is only 150 requests per hour per IP address NOT per domain. Or if you authenticate each request with OAuth you can make 350 requests per hour.

As my IP address is also shared by a lot of other sites that use this feature the Tweets may still not appear until I come up with a form of caching (which I haven't got round to doing) but the logic would be something like this.
  • Make the request to the new API URL.
  • Format the HTML using your callback function.
  • Make an AJAX request to a server side script (PHP/ASP/.NET etc) that stores the formatted HTML as a text/html file.
  • If you get back a 400 Status Code (Bad Request) which is the code Twitter send you when you exceed their rate limit. Then you can display this HTML file instead.


You can read more about rate limits here.

5 comments:

Anonymous said...

Hi, thanks for the explaination the new code. :)

I have a question: Is that code is limited about the number of request per day imposed by Twitter with their v1? Or I must save on my serveur and I refresh it every hour for exemple?

thanks for your return ;)

R Reid said...

Hi

I'm not really sure what you are asking.

Twitter limit the number of requests you can make to your timeline by the hour 150 not per day.

You should follow the logic at the bottom of the post and implement some form of caching to get round the problem of exceeding the rate limit.

So if you have your own server you can create a PHP/ASP file that you can call from the Twitter callback function with AJAX (passing the whole Twitter data that is returned to you in JSON format) and then this PHP/ASP file formats the data in the same way as the JavaScript version does and saves a file on your server somewhere.

Then in your callback function if you do exceed the rate limit (check for a 400 status code) you can use AJAX to load up the contents of this cached file instead so the Tweets always appear.

If you share a server like I do then the rate limit can be exceeded very quickly.

I hope this makes sense.

Anonymous said...

Hi,

for now, I simply use the twittercallback2 and I don't use cache, but after, I will use it for a website with 5k visitor/day, not in the home page but I think the limit 150/hour will explode

I will try a snippet like your help and I return to you

thanks a lot

ps: I don't use the API 1, is that important to use it when you use the callback?

R Reid said...

Hi

yes that would be great.

If you manage to get a caching system working then please send me the code. I really would like to have the spare time to work on one myself but I am too busy fixing other peoples plugins and working at the moment.

Thanks

joe web said...

your solution was exactly what i was looking for... simple to program and it worked perfectly. thank you!