How to use PHP to get the number of tweets for a URL?

Most of us are using the different widgets and plugins to display the number of Tweets/Retweets for our website URLs, blog post, articles, press release and other pages. Although there many other ways to keep a track on how many people are taking about your page on Twitter and how popular your page is online #SocialMedia.

Twitter also provides a small widget for adding Tweet Button to your website which allows your website users to easily share your web pages with their followers and it displays the number of people who have tweeted about that particular page. The Tweet Button count box shows how many times the URL has been Tweeted. You can choose to display or hide the count box, or place it above or next to the Tweet Button.

As the Tweet text are editable by design. The users are always able to change the text of a Tweet they are going to publish from their own account. But to avoid any kind of URL confusion and split in Tweet Count, it is always recommended to use the cleanest URL and avoid the tracking parameters. i.e. utm_source, utm_medium, utm_campaign etc. The default Tweet Button works without any configuration or parameters. The Tweet Button will use the URL of the current webpage and the content of the <title> element as the text of the Tweet.

Sometimes you many need the Tweet counts for internal reporting and analytics purpose and many not like to do it manually, by visiting each and every page. You can easily automate this process by using free API's and PHP Codes. Tweetmeme has an API by which you can find out how many times a specific URL has been Tweeted.

Example: http://api.tweetmeme.com/url_info?url=<YOUR_URL>

Below is the sample PHP code you can use to get the number of Tweet Count.

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;
    }
}

echo tweetCount('http://www.facebook.com/');

If you don't like to code or not able to run PHP for some reason you can also use the Twitter API. It also provides a way to get the number of retweets. Also I have created a free online tool to Count Tweet URLs, you can use this tool and let me know your feedback.

Example: http://urls.api.twitter.com/1/urls/count.json?url=www.facebook.com

You can use these tips & tricks to automate your #SocialMedia reporting process and save many hours of manual labor and be more accurate and efficient at work. This will help you make your MIS more solid and effective.

PHP Code Source: www.catswhocode.com

See also:

Joydeep Deb

Joydeep has 8+ years of strong experience in Online Marketing, Web Technologies, Search Engine Optimization, SEO, SEM, SMM, Web Master, eMail Marketing, Web Analytics, MIS Reporting, Web Design and Development.

Blog | Tools | MISC | Contact | G+

You May Also Like

One Response to “How to use PHP to get the number of Tweets for a URL?”

  1. Miky William says:

    We can use this script and use it to track/know how many times the important pages of our site are tweeted. Good script thanks for it.