Use your left/right keys to browse tutorials
Show Google Plus’s – PHP function

Show Google Plus’s – PHP function

1 Star2 Stars3 Stars4 Stars5 Stars
Posted on June 19, 2012

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'];

More tutorials from Papermashup
  • Nara L

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


  • Bradley

    Works like a charm :D

  • Jasa Web

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

  • Pingback: Pobranie ilości udostępnień danego URL dla Facebook, Twitter, Delicious, Pinterest, Google+ w PHP | Programming is fun

  • Pingback: Pixlatte Show Google Plus’s – PHP function | Pixlatte

  • Jan

    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

    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

      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

    Very useful code. Thanks for sharing.

  • phphunger

    wow nice post…

  • Maddy Vora

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

    Notice: Undefined index: result in C:\xampp\htdocs\demo\gplus.php on line 22
    and what is the unique API key og GOOGLE ?

    • SteeL

      u can get API key here