Papermashup

Subscribe


Tweets


"RT @kycutwilson: @ashleyford @burgerbeartom incredible. There's 5 more left! Shout about it!!"

@ashleyford 2 days ago

"@burgerbeartom @kycutwilson finally got around to using my free burgers for life! And we weren't disappointed. http://t.co/nJ6p2nLtg4"

@ashleyford 2 weeks ago

Designer and web developer, Co-founder and Technical Director at Harkable.com. Previously I worked at Spotify, MySpace and InMobi. Contact me - ashley[at]papermashup.com

Papermashup

Show Google Plus’s – PHP function

Use the Google Plus API to get the total number of shares for a URL

AshleyAshley

Following on from yesterdays function to get likes and share data for a URL here’s a function to get Google Plus’s for any URL.

The Code

It’s worth pointing out you must have CURL enabled on your web server, I believe it’s enabled by default on PHP5 setups. Also the Key below Isn’t a unique API key you must leave the key as is to ensure the code works properly.



function gplus_shares($url){

// G+ DATA
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://clients6.google.com/rpc?key=AIzaSyCKSbrvQasunBoV16zDH9R33D88CeLr9gQ");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, '[{"method":"pos.plusones.get","id":"p",
"params":{"nolog":true,"id":"' . $url . '","source":"widget","userId":"@viewer","groupId":"@self"},
"jsonrpc":"2.0","key":"p","apiVersion":"v1"}]');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));

$result = curl_exec ($ch);
curl_close ($ch);
return json_decode($result, true);

}

Example Usage



$gplus = gplus_shares('https://facebook.com');

echo $gplus[0]['result']['metadata']['globalCounts']['count'];

Designer and web developer, Co-founder and Technical Director at Harkable.com. Previously I worked at Spotify, MySpace and InMobi. Contact me - ashley[at]papermashup.com

Comments 12
  • Nara L
    Posted on

    Nara L Nara L

    Reply Author

    The code works like magic. Some other code I was using seems deprecated.

    Thanks.


  • Bradley
    Posted on

    Bradley Bradley

    Reply Author

    Works like a charm šŸ˜€


  • Jasa Web
    Posted on

    Jasa Web Jasa Web

    Reply Author

    thank you so much, this is work for my website…


  • Jan
    Posted on

    Jan Jan

    Reply Author

    Gosh.. can the reason be Iā€™m on a local virtual server (mamp)?
    I really copied your code as it is and I have
    A PHP Error was encountered
    Severity: Notice
    Message: Undefined index: result
    And yes, CURL is installed on my server


  • Jan
    Posted on

    Jan Jan

    Reply Author

    Here this code does not work neither.
    At developers.google.com/loader/signup they say no key is required any more. Maybe the old one in your code make it fails?


    • Ashley
      Posted on

      Ashley Ashley

      Reply Author

      Hi Jan,

      This code should work just fine. We use it in many applications, if you visit the homepage you can see the G+ count for each post which uses the exact code. You dont need to change the key. leave the code as it is.

      Ashley


  • saha
    Posted on

    saha saha

    Reply Author

    Very useful code. Thanks for sharing.


  • phphunger
    Posted on

    phphunger phphunger

    Reply Author

    wow nice post…


  • Maddy Vora
    Posted on

    Maddy Vora Maddy Vora

    Reply Author

    Hi,
    I am using this code but i get error like that which given below..

    Notice: Undefined index: result in C:xampphtdocsdemogplus.php on line 22
    and what is the unique API key og GOOGLE ?


    • SteeL
      Posted on

      SteeL SteeL

      Reply Author

      u can get API key here developers.google.com/loader/signup