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



More tutorials from Papermashup
Comments
12 discussions around Show Google Plus’s – PHP function
  1. Nara L says:

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

    Thanks.

  2. Bradley says:

    Works like a charm :D

  3. Jasa Web says:

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

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

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

  6. Jan says:

    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

  7. Jan says:

    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 says:

      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

  8. saha says:

    Very useful code. Thanks for sharing.

  9. phphunger says:

    wow nice post…

  10. Maddy Vora says:

    Hi,
    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 ?





Never miss an update from Papermashup

Get notified about the latest tutorials and downloads.

Subscribe by Email

Get alerts directly into your inbox after each post and stay updated.
Subscribe
OR

Subscribe by RSS

Add our RSS to your feedreader to get regular updates from us.
Subscribe

Get in contact

Please use the form below to get in contact. If your question is related to a free script download, please use the comments on the article page as community members are more likely to respond quicker than I can personally.

About Me

I'm Ashley Ford, Co-founder and Technical Director at Harkable.com London, UK. Previously I worked at InMobi, Spotify and MySpace. My interests include photography and making short videos I'm also an avid F1 fan. I'm always working on side projects. Here are a few: Easy Poll, We Deliver.



What do you specialise in?

I spend a lot of time coding in PHP and MySQL, as well as front end XHTML and CSS. I also specialise in javascript and the jQuery framework as well as being an avid designer. You can find me on dribbble

Interested in advertising?

If you'd like to advertise on Papermashup.com please get in touch via the contact link below for advertising opportunities.

How do I contact you

You can contact me here. and I'm available for consultation, freelance, programming book reviews.

Get on the mailing list

Join over 3000 people who have subscribed to the Papermashup inbox message, and be the first to find out about tutorial, competitions and giveaways.