[Drupal] How to find total count of tweets in Drupal site

| | 1 min read

In this article lets checkout how to fetch tweet count for a Drupal website. For this purpose we are using the Count API of twitter. Compared to Facebook on fetching likes and share counts we will notice a little difference. Simplest way to acquire the tweet count is to send request to the below url:

http://urls.api.twitter.com/1/urls/count.json?url=http://www.example-site.com/my-check

We will get json data as response for the above request, like below:


  {"count":1,"url":"http:\/\/wwwmydomain.com\/my-page"}

One drawback is that this API does not support batching requests.

This is one demo how the twitter's Count API is implemented,


  function tweetCount($url) {
    $content = file_get_contents("http://urls.api.twitter.com/1/urls/count.json?url=".$url);
    $content = json_decode($content);
    $retweets = $content->{'count'};
    if($retweets){
        return $retweets;
    } else {
        return 0;
    }
  }
  echo "Tweets: " . tweetCount('http://www.zyxware.com/');

Unlike count api there are various methods provided by twitter like api for fetching tweets, media functions like uploading images, posting data. You will be able to find more advanced features from twitter's developer site.

Hope this helps. Please fell free to share your thoughts and doubts regarding this here.