Get a URL Google+ Count Via PHP

<?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;
}
?>

← Back to home

One thought on “Get a URL Google+ Count Via PHP

  1. Hi, can you help me get the Google Plus count only for the last 7 days? What should I add to the code above? Thank you!

Comments are closed.