"@baekbunns This is utterly disgusting. Good for you for posting this. I don’t know you at all but nobody deserves t…"

@ashleyford 4 days ago

"@chrisallmark @officialUKMail Had exactly the same experience just last week, they are utterly useless."

@ashleyford 2 weeks ago

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


Show Google Plus’s – PHP function

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


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, "");
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"},
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('');

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

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

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.


  • 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 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.


  • 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

    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