How many times has an URL been tweeted?
You may have noticed how some websites let you know how many times URLs have been tweeted. How do they do that? The twitter api will help.
The the call we need is:
This call takes a
url as a parameter. Let’s look at an example:
This returns the following JSON (as of the time of writing), which we can parse to get the number we want:
The following snippet uses jQuery’s
$.getJSON method to call the twitter api using JSONP. Wire it up, define your callbacks and you’re away.
$.getJSON( "http://urls.api.twitter.com/1/urls/count.json", "callback=?&url="+encodeURIComponent("www.example.com")) .done( callbackSuccess ) .fail( callbackError );
The page www.phptherightway.com has been tweeted 5154 times.
Find out more: