If you want to get some of your social media statistics for a particular URL, like the number of Facebook “likes,” mentions on Twitter, or +1′s on Google Plus, you can (at least in theory). Facebook’s Open Graph API supports public like/share counts the best and in the easiest way. Twitter still allows you to do it by calling an internal JavaScript, and Google+ has no even semi-official way to do this, which is really uncharacteristic of them.

Google even took measures to disable the “unofficial” API method, so the only way to do it at the moment is to rip the number out of the official Google+ button’s source code with a regular expression. You can complain on this issue ticket and if enough people do, they might actually listen.

In any case, here are the good, the bad, and the ugly:

The Good: Count Facebook Likes

You just give it a url (for shares) or an id (for likes) and it pulls the JSON directly and parses it to return a count. Beautiful.

function shinra_fb_count( $url_or_id ) {
    /* url or id (for optional validation) */
    if( is_int( $url_or_id ) ) $url = 'http://graph.facebook.com/' . $url_or_id;
    else $url = 'http://graph.facebook.com/' .  $url_or_id;

    /* get json */
    $json = json_decode( file_get_contents( $url ), false );

    /* has likes or shares? */
    if( isset( $json->likes ) ) return (int) $json->likes;
    elseif( isset( $json->shares ) ) return (int) $json->shares;

    return 0; // otherwise zed
}

The Bad: Twitter Count URL in Tweets

This will return a count of how many tweets contain the URL you pass to the function. It uses a component of the tweet widget script, and therefore it most likely isn’t officially supported. However, it’s an even simpler JSON call.

function shinra_twitter_count( $url ) {
    /* build the pull URL */
    $url = 'http://cdn.api.twitter.com/1/urls/count.json?url=' . urlencode( $url );
 
    /* get json */
    $json = json_decode( file_get_contents( $url ), false );
    if( isset( $json->count ) ) return (int) $json->count;
 
    return 0; // else zed
}

The Ugly: Google Plus +1′s Count

This is an ugly hack and undoubtedly not supported by Google. But it’s the only way I could think of if you absolutely can’t just use the official button:

function shinra_gplus_count( $url ) {
    /* get source for custom +1 button */
    $contents = file_get_contents( 'https://plusone.google.com/_/+1/fastbutton?url=' .  $url );

    /* pull out count variable with regex */
    preg_match( '/window\.__SSR = {c: ([\d]+)/', $contents, $matches );

    /* if matched, return count, else zed */
    if( isset( $matches[0] ) ) 
        return (int) str_replace( 'window.__SSR = {c: ', '', $matches[0] );
    return 0;
}

A Word of Caution

Aside from the first Facebook method, these calls work in a way that the developers almost certainly didn’t intend and are unlikely to be stable for years to come. It may also be a violation of their respective API Policies or Terms of Use that you might have agreed to, so keep in mind that just because you can do it doesn’t necessarily mean that you should do it.

5 Thoughts on “Count Facebook Likes, Twitter Links, and Google Pluses with PHP

  1. also you can use fql to retrieve facebook like count: http://newexception.com/facebook-like-counter

  2. What’s up, just wanted to tell you, I prefer this story. It was the best. Keep on posting! michael kors outlet online http://www.harrisbranch.com/

  3. Pingback: Difficulty getting google plus one count - PHP Solutions - Developers Q & A

  4. Thanks for Google Plus +1′s Count.

  5. nice tricks, i’m going to use the g+ code because i find no other way to do it, i’m not php coder i just gonna play with :) thanks for the post

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>

Post Navigation