Twitter’s New Tweet Button and the Count API
So I added the new Tweet button to Simple Twitter Connect yesterday, and was going through the documentation they provided for it. One thing I noticed was a mention of the “count API”.
Now, Twitter says that the count API is private, but come on.. Seriously? You’re sending count information to every Tweet button being displayed anywhere, how private can that really be?
Turns out it’s not private at all. It’s just hidden.
To get the tweet count for any URL, simply do a GET for this:
What you’ll get back will look like this:
Some of you may recognize that as a JSON callback. Basically, they’re using part of their @anywhere system to receive the count and update the button accordingly. Not surprising, the whole tweet button is built on top of the anywhere code. But it’s kinda useless to us in this format, since we need to be able to define our callback appropriately instead of using theirs. No worries, just add a callback parameter yourself:
Obviously, they didn’t release this information because they’re concerned about scaling, and probably rightfully so. Still, if you want that count, you can get it. Just don’t abuse it.
If you take the callback off, it’ll provide just JSON.
However, they’ll likely ban you if they see you abusing the service.
True, but then you’re assuming they can detect it. Every load of the official tweet button hits this service to get that count.
Oh, by abuse, I mean 1000 calls per day on the same IP address (for say, a data aggregator).
Wouldn’t a site like nytimes.com for example generate tens if not hundreds of thousands of calls to that from the same IP per day (OK, less per url argument, but still..).
I’ve been struggling with this for a couple of days and can’t get it working. Could you post a code example? Something along the lines of getting the count and putting it into a variable would be wonderful. Please help 🙁
Well, that depends on what exactly you want to do. Do you want to get the count in PHP, like with WordPress? If so, something like this would do the trick:
Then you could call it with $count = get_tweet_count(get_the_permalink()) or something similar to get the count. You’ll probably want to cache the resulting value in a transient or something, so you don’t hit twitter’s servers every single time you need the count.
I was just looking for something like this. Amazing. Thank you.
Would you care to share how you’d create the call (I want to hard-code it in a sidebar)? I must admit that my attempts have been unsuccessful so far.
This is so cool! However, when I use your function in WordPress it returns a count of 0 every time. I can check it manually and see that I have a count but the function always returns 0. What am I missing?
Tested, and the function works correctly as far as I can see.
Thanks for the reply and feedback.
This is the code I have for my WordPress loop and function:
It always returns 0. Am I missing something?
Yes, you’re missing two things.
One is that your if and else (on the $count==0) have the same code in them.
Second is that this line of code is wrong:
The the_permalink() function echo’s the permalink to the output. It does not return it as a string to be passed to another function. Change that line to this:
GENIUS!! Thanks so much. I actually pasted the wrong if/else statement in the topic above. My problem was the_permalink vs. get_permalink. Thanks again!
Your suggestion is very helpful for my project.
Thank you very much.
What you do is to create the DIV somewhere where you want it, then use the callback method I mention above to get the count and put it into the DIV.
I’ve used your example with success. Big thanks for your post.
Still, I have a question… Do you know a way to retrieve the tweetcount for all urls on a complete domain?
So for instance. let’s say I have three urls with all a tweetcount:
Retrieving it with the domain url (first url), as expected, would give me back a count of 3.
Now is there a way to retrieve the total tweetcount of 15? for the domain. Thus retrieving a count per domain, not per url?
Is there a twitter api method for this?
Hi mate, I’m using WordPress and I pasted your function into my functions.php file, then your
$count = get_tweet_count(get_the_permalink())but it gave an error:
Fatal error: Call to undefined function get_the_permalink()so I google’d it and got this post, which says to use
$permalink = get_permalink($post->ID);instead, but I’m not much of a PHP guru and don’t know where to put that. At the moment I’ve got
inside the loop in index.php but it’s not returning anything.
Can you help?
This is a great find and writeup. Thanks.
Good find – thank you for posting that!
I’ve added support for this to Jtwitter, the Java library for Twitter which I maintain.
how accurate is this api? i read somewhere that sometimes return wrong number of tweet count.
found this on my old blog: https://blog.htmlviewer.net/tweet-counter , just not sure if api still works.
hey guys I just need the same type of code for displaying count of tweets/posts by an user on my web application using php code(not a wordpress site),how can i use this code or is there any examples or code for that, please help me…