Count the number of times your page is Tweeted via the TweetMeme API

TweetMeme is an excellent site that offers various types of Twitter-based social integration to your website. They’re particularly relevant when it comes to finding current news of interest because they measure the number of times a certain website has had their story published or tweeted to Twitter. Those stories that have the most tweets are featured on the front page of the their website.

I like to know how many times certain pages from certain websites are published to Twitter, and TweetMeme’s API makes it easy to find that information. It’s also nice to be able to extract that information and publish the number of tweets on my own page in a format that best fits my own website.

The TweetMeme API will return an XML document that you can style in any way you wish. You can include the number of tweets on your page in any format and, if you so choose, link back to your stories page on the tweetmeme.com website.

This PHP function will output only one element as text – the tweet count.

<?php
function tweetCount($url) {
    $content = file_get_contents("http://api.tweetmeme.com/url_info?url=".$url);
    $element = new SimpleXmlElement($content);
    $retweets = $element->story->url_count;
    if($retweets){
        return $retweets;
    } else {
        return 0;
    }
}
?>

I use this API integrated with another application to aggregate the popularity of posts over 40 of my websites. It sends me a nice summary every night and posts the data to a database so I can measure the success or failure of particular types of information.

The XML output will look like this:

tweetmem api url count

The Tweetmeme URL count is highlighted

The output of the PHP function will output a plain text number.

Usage

Usage is easy.

<?php
echo tweetCount("http://www.flightpodcast.com/episode-6-john-bartels-qantas-qf30");
?>

If you have any questions regarding what I’ve described above, please email or leave a comment. I’ll be happy to help


I’ll publish some WordPress shortcode shortly that enables dynamic count data to be parsed in posts as text.

You can download the above code below.

First Name:
Your Email Address:
 




Download: Tweetmeme URL Count
Description: Tweetmeme URL Count
Author:Marty
Category: PHP code
Date: May 8, 2011



If you liked this article, you may also like:

  1. Include Twitter Data on your Website
  2. Count the Number of Shares to Facebook, Twitter, LinkedIn & Google Plus
  3. Automate WordPress Posts to Twitter (with hastags, truncation and a short URL)
  4. [Shortcode] Examples [1]
  5. Post to Twitter using OAuth
About Marty

is a passionate web developer from Sydney, Australia. He owns about 600 websites and makes a healthy living from working the web. As a day job, he works as a pilot for an international airline. Follow Marty on Twitter or Google+.

Comments

  1. Kate says:

    I’ve been trying to get this to work on my homepage which shows multiple posts.
    It throws up errors saying your can’t re use the same function… any thoughts?

  2. Marty says:

    Keep in mind that the above code is really only useful for development purposes. If you’re interested in a tweet button, perhaps you might just want to use one of TweetMeme’s retweet buttons.

    If you wanted use of the function for other purposes, send me a message via the form at the top of this page with the error message you’re receiving and I’ll do what I can to help. Provide as much information as possible. Alternatively, my email is marty @ [this domain name].

Trackbacks

  1. [...] – how to render that data on your screen in a small text box. I’ve posted about Twitter counts before in isolation; this post expands upon the same [...]

Speak Your Mind

*