How to Get the Number of Retweets in PHP

How to Get the Number of Retweets in PHP thumbnail
See how many times a Top Tweet was retweeted on Twitter.

You can use the PHP hypertext preprocessor language to quickly retrieve the number of retweets for a particular tweet or link on the Twitter social networking site. You will need access to the TweetMeme API, because it offers a method to retrieve tweet count numbers with little difficulty. TweetMeme is a service that tracks Twitter users and messages. The part of the API you will use in this method is public. For a deeper analysis of Twitter usage, you can sign up for TweetMeme's paid Analytics API service. However, for this PHP code, no TweetMeme account is necessary.

Instructions

    • 1

      Open a new PHP document and insert the following code near the top, before the HTML "<body>" tag:

      <?php

      function tweetCount($URL) {

      $content = file_get_contents("http://api.tweetmeme.com/url_info?URL=".$URL);

      $element = new SimpleXmlElement($content);

    • 2

      Type or insert the next lines of code below the first:

      $retweets = $element->story->url_count;

      if($retweets){

      return $retweets;

      } else {

      return 0;

      }

      }

      ?>

    • 3

      Go to the body section of the document and type the following PHP code anywhere between "<body>" and "</body>" tags:

      <?php

      echo "Retweeted "; tweetCount("http://example.com/link.html");

      echo "times on Twitter."

      ?>

    • 4

      Replace the "example.com/link.html" with the actual URL for which you want to retrieve a retweet count.

    • 5

      Save the PHP document and upload it to your Web hosting account.

    • 6

      Visit the Web page to view your content with the Twitter retweet count. It should read, "Retweeted (number) times on Twitter" with (number) replaced by the actual amount a link has been retweeted on Twitter.

Related Searches:

References

  • Photo Credit Oli Scarff/Getty Images News/Getty Images

Comments

Related Ads

Featured