Display the Number of Retweets for a Post Using PHP

Tweeting is what we do after publishing posts. To place some minimal text that jumps upon the Tweet on Twitter will be more great instead of using a tweet button. Its made possible to count the number of tweets using PHP only, however if you want to sizzle it up, then its your way.

We have two ways of displaying number of Tweets for particular posts.

Using Tweetmeme API Key

Tweetmeme provides you an API key using which you can do more with it. Jean Paul of CatsWhoCode made his way of displaying the number of Retweets.  What you have to do is place the code below in your theme functions.php file.

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

After placing the code, save the file and place the below echo to display it anywhere on your site.

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

Using Your Backtype API Key

Backtype is a nice source from where you can fetch the number of tweets of a post as it collects all the data of it.

<?php
$link = get_permalink($post->ID);
$key = 'YOURKEY';
$url = "http://api.backtype.com/tweetcount.xml?q=$link&amp;amp;key=$key";
$request = new WP_Http;
$result = $request->request( $url );
echo "<span class=\"result\">".$result['body']."</span> tweets";
?>

If you want to link it with twitter, then you can use the following.

$link = get_permalink($post->ID);
$key = 'YOURKEY';
$url = "http://api.backtype.com/tweetcount.xml?q=$link&amp;amp;key=$key";
$request = new WP_Http;
$result = $request->request( $url );
$json = $result['body'];
$burl = getBitlyUrl(get_permalink($post->ID));
$posttitle = get_the_title();
echo "<span class=\"result\"><a href=\"http://twitter.com/home?status=RT @smashingeeks $posttitle: $burl \">" .$result['body']. "</span></a> tweets";
?>

Do not forget to change the YOURKEY with your API key of Backtype. Place the code anywhere you want to display the whole.

If there are more ways of doing it, you can share with us by comments.

Comments

  1. Hellen says

    Great items from you, man. I have remember your stuff previous to and you are just too great. I actually like what you’ve acquired right here, certainly like what you’re saying and the best way by which you are saying it. You make it entertaining and you still take care of to keep it smart. I can not wait to read far more from you. This is actually a terrific site.

Trackbacks

  1. [...] Display the Number of Retweets for a Post Using PHP [...]

  2. Display the Number of Retweets for a Post Using PHP | GARBAGE ENGINE™ | The best design resources on the web says:

    [...] text that jumps upon the Tweet on Twitter will be more great instead of using a tweet button [source] Internet Useful development | php, tricks, twitter [...]

  3. [...] Display the Number of Retweets for a Post Using PHP [...]

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>