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

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.


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

function tweetCount($url) {
    $content = file_get_contents("".$url);
    $element = new SimpleXmlElement($content);
    $retweets = $element->story->url_count;
        return $retweets;
    } else {
        return 0;

echo tweetCount('');

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.


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:

See also:

About the Author: Joydeep Deb

Joydeep Deb is a Senior Digital Marketer and Technical Marketer with 12+ years of experience in Digital Marketing, Lead Generation, Online Brand Management, Marketing Campaigns, Search Engine Optimization (SEO), Search Engine Marketing (SEM), PPC, eMail Marketing, Web Analytics, Web Technologies, Web Design and Development.

Blog | Tools | MISC

Additional Posts

The Mind Boggling Question is…Can the Creativity Pyramid and Maslow’s Hierarchy Save Your Blog Traffic?

10 Most Outrageous Pranks Played on Google

Unify Your Social Media Strategy In 7 Simple Steps

In compliance with Ontario’s non-essential business closure our physical offices are closed until further notice. Fortunately our willingness to adopt work from home and the required technology over the past two years has allowed us to continue our operations without impact. For our valued clients and partners you can expect the same great level of service and execution you have become accustomed to.

Many clients/prospects have reached out to us in an effort to introduce new campaigns as quickly as possible. In an effort to help our clients pivot we have increased our campaign build capacity. We are now able to turn new campaigns over in 2-3 business days opposed to the typical 5-7 business day turnaround time. Please note that campaign launch approvals from the vendor side (Google, Bing, Facebook, Instagram etc.) may be delayed as those companies migrate to work from home.

For existing clients please reach out to your account manager with any questions you may have.

For non-clients looking for assistance with new campaigns please Contact Us

Read previous post:
The Mind Boggling Question is…Can the Creativity Pyramid and Maslow’s Hierarchy Save Your Blog Traffic?

When I asked one of our blog readers what he finds most challenging about creating and maintaining a successful blog,...Read...