Accessing the Google API with PHP
Google makes its search functions available to you in the form of an API which you can call from PHP scripts using SOAP. The easiest way to use SOAP with PHP is to download a package called NuSoap. NuSoap consists of one file, nusoap.php which contains classes and functions that you can use to call web services with the SOAP protocol. The following code demonstrates how to obtain the Google ranking for a keyword, given the keyword and url of the website. You would use the code as follows:
$search = new googleSearch("php help", "www.php-help.org");
$search_result = $search->position();
The class definition is as follows:
class googleSearch {
var $api_key = 'xxxxxxxx'; // supply your google API key here
var $results;
var $offset = 0;
var $url;
function googleSearch($keyword, $url) {
require_once('nusoap.php');
$wsdl="GoogleSearch.wsdl";
$client=new soapclient($wsdl, 'wsdl');
$this->url = $url;
for ($s = 0; $s < 200; $s += 10) {
$param=array(
'key'=>$this->api_key,
'q'=>$keyword,
'start'=>$s,
'maxResults'=>10,
'filter'=>'false',
'restrict'=>'',
'safeSearch'=>'false',
'lr'=>'',
'ie'=>'latin1',
'oe'=>'latin1'
);
for ($i = 0; $i < 100; $i++) {
$this->results = $client->call('doGoogleSearch', $param);
if ($this->results) break;
}
$res = $this->position($url);
$rank = substr($res, 0, strstr($res, ';') + 1);
if ($rank != 0) {
$this->offset = $s;
break;
}
echo '.'; flush();
}
if (!$this->results) echo 'You have reached the Google maximum of 1000 queries/day... ';
}
function resultCount() {
return $this->results["estimatedTotalResultsCount"];
}
function position() {
$listings = $this->results["resultElements"];
$size = sizeof($listings);
$pos = -1;
for ($i=0; $i < $size; $i++) {
if (strstr($listings[$i]["URL"], $this->url) != false) { $pos = $i; break; }
}
return $this->offset + $pos + 1 . ';' . $listings[$pos]["URL"];
}
}
$search = new googleSearch("php help", "www.php-help.org");
$search_result = $search->position();
The class definition is as follows:
class googleSearch {
var $api_key = 'xxxxxxxx'; // supply your google API key here
var $results;
var $offset = 0;
var $url;
function googleSearch($keyword, $url) {
require_once('nusoap.php');
$wsdl="GoogleSearch.wsdl";
$client=new soapclient($wsdl, 'wsdl');
$this->url = $url;
for ($s = 0; $s < 200; $s += 10) {
$param=array(
'key'=>$this->api_key,
'q'=>$keyword,
'start'=>$s,
'maxResults'=>10,
'filter'=>'false',
'restrict'=>'',
'safeSearch'=>'false',
'lr'=>'',
'ie'=>'latin1',
'oe'=>'latin1'
);
for ($i = 0; $i < 100; $i++) {
$this->results = $client->call('doGoogleSearch', $param);
if ($this->results) break;
}
$res = $this->position($url);
$rank = substr($res, 0, strstr($res, ';') + 1);
if ($rank != 0) {
$this->offset = $s;
break;
}
echo '.'; flush();
}
if (!$this->results) echo 'You have reached the Google maximum of 1000 queries/day... ';
}
function resultCount() {
return $this->results["estimatedTotalResultsCount"];
}
function position() {
$listings = $this->results["resultElements"];
$size = sizeof($listings);
$pos = -1;
for ($i=0; $i < $size; $i++) {
if (strstr($listings[$i]["URL"], $this->url) != false) { $pos = $i; break; }
}
return $this->offset + $pos + 1 . ';' . $listings[$pos]["URL"];
}
}
Need assistance with your project? Universal Web Services can help.
Contact us to request a quote.