Getting Google Plus Count Using PHP and CURL

One of the latest and very helpful social button is the Google Plus Button. It is very helpful because it affects the SEO points of the website in Google Algorithm since its owned by the Google Company.

Here’s one great tutorial that gets the Google Plus Button Count Using PHP and CURL. You need to enable your curl on your server to make the codes work. To do that, you need to update your php.ini file then try to search and uncomment this line.

;extension=php_curl.dll

When you have already set your CURL on your server, you can try to do this. Here is the sample code for the Google Plus Button Count.

$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);

The code above gets the count of the Google Plus Buttons of a specific url. We can use a JSON Decode function to get the data from the API Site.

Lets make a PHP Function that will do all the process above. Check this out.

PHP CODE:


//Get count of g+ of URL
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);

$json = json_decode($result, true);
return intval($json[0]['result']['metadata']['globalCounts']['count']);

}

Now we have a function, we can use it as much as we can. Here’s the sample code on how we can echo the number of count.

HOW TO VIEW IT:

//GPlus count
$gplus_shares = gplus_shares($url);

echo $gplus_shares;

Sparky! We can now get the Google Plus Count of the URL Using a PHP Function, Curl, and JSON. Help and let others know this tutorial and Share the knowledge.

DEMO DOWNLOAD

Incoming search terms:

wilbert Santos

He is the founder and the man behind Upgraded Tutorials that focuses on creative and helpful web development tutorials. He is a Web Developer and knows CodeIgniter, WordPress and other frameworks. He loves web development and creating cool stuff. Add him on Facebook and Google

Worth Reading Topics

  • http://www.gentlemedia.nl/ Ralph Echter

    Thanks for the tut and I’m thinking of integrating it. I have a question. Does this script shares the page with the url automatically? I’m experimenting with a project that uses the history API and some AJAX to load content and I need to define manually the url that needs to be shared. Where can I change that in this script? Can I set a variable something like this?:

    $url = “http://www.page,php”;

    Thanks!

  • http://www.gentlemedia.nl/ Ralph Echter

    you can delete my posts… I figured it out already… thnx