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:

http://urls.api.twitter.com/1/urls/count.json?url=URL-TO-GET-COUNT-FOR

What you’ll get back will look like this:

twttr.receiveCount({"count":123,"url":"URL-TO-GET-COUNT-FOR"})

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:

http://urls.api.twitter.com/1/urls/count.json?url=URL-TO-GET-COUNT-FOR&callback=bob

bob({"count":123,"url":"URL-TO-GET-COUNT-FOR"})

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.

Shortlink:

23 Comments

  1. If you take the callback off, it’ll provide just JSON.
    However, they’ll likely ban you if they see you abusing the service.

  2. 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:

      function get_tweet_count($url) {
      	$count = 0;
      	$data = wp_remote_get('ht'.'tp://urls.api.twitter.com/1/urls/count.json?url='.urlencode($url) );
      	if (!is_wp_error($data)) {
      		$resp = json_decode($data['body'],true);
      		if ($resp['count']) $count = $resp['count'];
      	}
      	return $count;
      }
      

      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.

        Cheers

      • 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:

            LOOP

            <!-- TWEET BUTTON - HIDE COUNT IF THE COUNT IS 0 --<
            <?php $count = get_tweet_count(the_permalink()); ?>
            <?php if ($count == 0){ ?>
            	<script src="http://platform.twitter.com/widgets.js" type="text/javascript"></script>
            	<a href="http://twitter.com/share" class="twitter-share-button"
            	  data-url="<?php the_permalink(); ?>"
            	  data-via="TITLE"
            	  data-text="<?php the_title(); ?>"
            	  data-related=""
            	  data-count="horizontal">Tweet</a>							
            <?php } else { ?>
            	<script src="http://platform.twitter.com/widgets.js" type="text/javascript"></script>
            	<a href="http://twitter.com/share" class="twitter-share-button"
            	  data-url="<?php the_permalink(); ?>"
            	  data-via="TITLE"
            	  data-text="<?php the_title(); ?>"
            	  data-related=""
            	  data-count="horizontal">Tweet</a>							
            <?php } ?>
            

            FUNCTION

            function get_tweet_count($url) {
            	$count = 0;
            	$data = wp_remote_get('ht'.'tp://urls.api.twitter.com/1/urls/count.json?url='.urlencode($url) );
            	if (!is_wp_error($data)) {
            		$resp = json_decode($data['body'],true);
            		if ($resp['count']) $count = $resp['count'];
            	}
            	return $count;
            }
            ?>
            

            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:

              <?php $count = get_tweet_count(the_permalink()); ?>
              

              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:

              <?php $count = get_tweet_count(get_permalink()); ?>
              
    • On the other hand, if you want to use it in javascript, well then that’s exactly what the callbacks are for.

      jQuery.getJSON(
      	'ht'+'tp://urls.api.twitter.com/1/urls/count.json?url=http://ottopress.com/2010/twitters-new-tweet-button-and-the-count-api/&callback=?', 
      	function (data) {
      		// do something to do what you want with data.count here
      		alert(data.count);
      	}
      );
      
  3. I was trying to obtain it using javascript only, setting a js variable, then I would create a DIV element, put the count there and forget about it. I can’t work with php (at least I think so) because I’m doingg this for Blogger. Just trying to make a custom twitter buttom WITH counter. jQuery isn’t helping me much, i’m afraid. Thank you for the quick reply!

  4. 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:

    http://www.example.com/ 3
    http://www.example.com/b 5
    http://www.example.com/c 7

    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?

    Thanks!

  5. 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?

  6. This is a great find and writeup. Thanks.

  7. Good find – thank you for posting that!

    I’ve added support for this to Jtwitter, the Java library for Twitter which I maintain.

  8. how accurate is this api? i read somewhere that sometimes return wrong number of tweet count.

  9. 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…

    Thank you

    best regards,
    Vijay m

Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Need to post PHP code? Wrap it in [php] and [/php] tags.