Google-plus-one-spam

Just a quick post for all you developers out there – I’ve quickly hacked together a function for getting the number of shares of a url on Google+. I can’t be the only one out there who needs this, so I thought I’d give back to the community with it. This implimentation is in PHP, but it shouldn’t be too hard to understand and port.


<?php
$ch = curl_init();

$encUrl = "https://plusone.google.com/u/0/_/+1/fastbutton?url=".urlencode($url)."&count=true";

$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => 'spider', // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 5, // timeout on connect
CURLOPT_TIMEOUT => 10, // timeout on response
CURLOPT_MAXREDIRS => 3, // stop after 10 redirects
CURLOPT_URL => $encUrl,
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => false,
);

curl_setopt_array($ch, $options);

$content = curl_exec($ch);
$err = curl_errno($ch);
$errmsg = curl_error($ch);

curl_close($ch);

if ($errmsg != '' || $err != '') {
print_r($errmsg);
print_r($errmsg);
}
else {
$dom = new DOMDocument;
$dom->preserveWhiteSpace = false;
@$dom->loadHTML($content);
$domxpath = new DOMXPath($dom);
$newDom = new DOMDocument;
$newDom->formatOutput = true;

$filtered = $domxpath->query("//div[@id='aggregateCount']");
return $filtered->item(0)->nodeValue;
}
?>

Enjoy!

Get a URL Google+ Count Via PHP by