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.
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);
}
$gplus = gplus_shares('https://facebook.com');
echo $gplus[0]['result']['metadata']['globalCounts']['count'];
Works like a charm
thank you so much, this is work for my website…
Pingback: Pixlatte Show Google Plus’s – PHP function | Pixlatte
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
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?
Very useful code. Thanks for sharing.
wow nice post…
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 ?
u can get API key here developers.google.com/loader/signup
Designer and web developer, Co-founder and Technical Director at Harkable.com London. Previously I worked at InMobi, Spotify and MySpace. Interests include photography and making short videos. Also an avid F1 fan. I also run Mega Infographics for your daily dose of the best infographics.
Follow us on Twitter and get in-stream updates.
Subscribe to all the Papermashup tutorials and articles straight to your RSS reader.
Sign up and get all the Papermashup tutorials straight to your inbox.
11 discussions around Show Google Plus’s – PHP function