I've written a PHP function to get plus ones count for a URL
function makeApiCall($destinationUrl, $stringOfParams){
$curl = curl_init();
echo $destinationUrl.$stringOfParams."<br>";
curl_setopt($curl, CURLOPT_URL, $destinationUrl.$stringOfParams);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
echo $result;
}
While inputting https://plusone.google.com/u/0/_/+1/fastbutton
as destination URL and inputting the correct string of params, the result I'm receiving in $result
is HTML. The problem is that I would like using PHP to get the count and not using JavaScript.
How can I do that?
Using preg_match
, you can achieve it.
Assuming you are calling a url like that :
https://plusone.google.com/_/+1/fastbutton?bsv=pr&url=http://www.google.com
You are looking for:
<div id="aggregateCount" class="t1">118k</div>
or
<div id="aggregateCount" class="t1">12</div>
So you can perform:
preg_match('/\<div id=\"aggregateCount\" class=\"t1\"\>\>?([0-9]*k?)\<\/div\>/i', $result, $matches);
And $matches
will be:
Array
(
[0] => <div id="aggregateCount" class="t1">118k</div>
[1] => 118k
)
edit:
After running the example, it seems that Google return a different number when using curl, for example, on http://www.google.com
, it returns:
<div id="aggregateCount" class="t1">>9999</div>
So I've updated the regex to handle the >
.