Google plus counter

Google plus counter

In order to get the number of google plus shares for specific url you will need to send the following json string through a POST method:

[
    {
        "method":"pos.plusones.get",
        "id":"p",
        "params":{
                     "nolog":true,
                     "id":"URL",
                     "source":"widget",
                     "userId":"@viewer",
                     "groupId":"@self"
                 },
        "jsonrpc":"2.0",
        "key":"p",
        "apiVersion":"v1"
    }
]
function google_plus_counter($url){
 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, "https://clients6.google.com/rpc");
    curl_setopt($ch, CURLOPT_POST, 1);
    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_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));

    $response = curl_exec ($ch);
    curl_close ($ch);
    return json_decode($response, true);

}

$response = google_plus_counter('http://php.net/');
print_r($response);
echo "Shares: " . $response[0]['result']['metadata']['globalCounts']['count'];

Output:

Array
(
    [0] => Array
        (
            [id] => p
            [result] => Array
                (
                    [kind] => pos#plusones
                    [id] => http://php.net/
                    [isSetByViewer] => 
                    [metadata] => Array
                        (
                            [type] => URL
                            [globalCounts] => Array
                                (
                                    [count] => 1213
                                )

                        )

                    [abtk] => AEIZW7TStmKB+EsyBy8MM8z2gnjs6rWVEIZ7I1gXZ6QC9RTYRRyc293J9nitm7oXhdvFcVhXZ2NE
                )
        )
)

Shares: 1213

See also: Get the number of facebook likes

- Posted by Eva to Php