The RPC API was never meant for public use and Google changed authentication in order to prevent abuse. Thus, the code that you posted does not work any longer. However, I found a much simpler solution:
Update (23.01.2013): Google blocked this URL in december 2012 - so this method doesn't work any longer!
Update (15.05.2013): The method works again!
<?php
/**
* Get the numeric, total count of +1s from Google+ users for a given URL.
* @author Stephan Schmitz <[email protected]>
* @copyright Copyright (c) 2013 Stephan Schmitz
* @license http://eyecatchup.mit-license.org/ MIT License
* @link <a href="https://gist.github.com/eyecatchup/8495140">Source</a>.
* @param $url string The URL to check the +1 count for.
* @return intval The total count of +1s.
*/
function getGplusShares($url) {
$url = sprintf('https://plusone.google.com/u/0/_/+1/fastbutton?url=%s', urlencode($url));
preg_match_all('/{c: (.*?),/', file_get_contents($url), $match, PREG_SET_ORDER);
return (1 === sizeof($match) && 2 === sizeof($match[0])) ? intval($match[0][1]) : 0;
}
Update (18.01.2014): Here's an improved version that uses curl, a fallback host and does some error handling (the latest version can be found here https://gist.github.com/eyecatchup/8495140).
<?php
/**
* GetPlusOnesByURL()
*
* Get the numeric, total count of +1s from Google+ users for a given URL.
*
* Example usage:
* <code>
* $url = 'http://www.facebook.com/';
* printf("The URL '%s' received %s +1s from Google+ users.", $url, GetPlusOnesByURL($url));
* </code>
*
* @author Stephan Schmitz <[email protected]>
* @copyright Copyright (c) 2014 Stephan Schmitz
* @license http://eyecatchup.mit-license.org/ MIT License
* @link <a href="https://gist.github.com/eyecatchup/8495140">Source</a>.
* @link <a href="https://mcmap.net/q/168360/-getting-google-plus-shares-for-a-given-url-in-php">Read more</a>.
*
* @param $url string The URL to check the +1 count for.
* @return intval The total count of +1s.
*/
function GetPlusOnesByURL($url) {
!$url && die('No URL, no results. ;)');
!filter_var($url, FILTER_VALIDATE_URL) &&
die(sprintf('PHP said, "%s" is not a valid URL.', $url));
foreach (array('apis', 'plusone') as $host) {
$ch = curl_init(sprintf('https://%s.google.com/u/0/_/+1/fastbutton?url=%s',
$host, urlencode($url)));
curl_setopt_array($ch, array(
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 6.1; WOW64) ' .
'AppleWebKit/537.36 (KHTML, like Gecko) ' .
'Chrome/32.0.1700.72 Safari/537.36' ));
$response = curl_exec($ch);
$curlinfo = curl_getinfo($ch);
curl_close($ch);
if (200 === $curlinfo['http_code'] && 0 < strlen($response)) { break 1; }
$response = 0;
}
!$response && die("Requests to Google's server fail..?!");
preg_match_all('/window\.__SSR\s\=\s\{c:\s(\d+?)\./', $response, $match, PREG_SET_ORDER);
return (1 === sizeof($match) && 2 === sizeof($match[0])) ? intval($match[0][1]) : 0;
}
Update (02.11.2017): The +1 count is officially dead! As announced in this Google+ Post by Product Manager John Nack Google recently removed the share count (aka +1 Count) from their web Sharing buttons. (They claim that the purpose of this move is for making the +1 button and share box load more quickly.)