Using Google’s Ajax Search API with PHP and Perl
Since I feel more proficient in Perl then other languages, I began writing a script in Perl to automatically run searches on Google’s web database. See http://code.google.com/apis/ajaxsearch/. However, I quickly discovered that the required modules didn’t work. The Perl v5.8.8 x86_64-linux-gnu-thread-multi version of the program would not run on my apache 2.2.8 web server on Ubuntu 8.04 Hardy Heron. The level of dependencies required by the Google search.pm module was too deep to be solved by using the lib statement and placing a few modules under the root of the web server. Other JSON modules on CPAN were useless because they had the same depencies. Although I could have eventually fixed the module issue on my local box, I wouldn’t be able to do it to the rented shared hosting server where mikeskramstad.com resides. Below are the Perl code snippets and the error they produced.
use lib ‘/home/users/web/b1548/pow.skramstad/htdocs/perl/lib2′;
use Google::Search;
$search = Google::Search->Web(q => $keywords, key => $key, referer => $referer);
$result = $search->first;
while ($result) {
if ($result->uri =~ /$target/) {
print “found “, $result->uri , ” at position “, ($result->number + 1), “\n”;
print
exit 0;
}
$result = $result->next;
}
Error: You cannot overwrite a locally defined method (reason) with an accessor at /usr/local/share/perl/5.8.8/Moose/Meta/Attribute.pm line 584
PHP came to the rescue with its builtin curl statement and hardly any messing around with modules. The code below came from Google’s example page.
function retrieveGoogleSearch($searchTerms,$searchURL, $start) {
$googleBaseUrl = “http://ajax.googleapis.com/ajax/services/search/web”;
$googleBaseQuery = “?start=$start&rsz=large&v=1.0&q=”;
$googleFullUrl = $googleBaseUrl . $googleBaseQuery . $searchTerms;
// $searchURL .
$curlObject = curl_init();
curl_setopt($curlObject,CURLOPT_URL,$googleFullUrl);
curl_setopt($curlObject,CURLOPT_RETURNTRANSFER,true);
curl_setopt($curlObject,CURLOPT_HEADER,false);
curl_setopt($curlObject,CURLOPT_REFERER,”http://www.mikeskramstad.com”);
$returnGoogleSearch = curl_exec($curlObject);
curl_close($curlObject);
$returnGoogleSearch = json_decode($returnGoogleSearch,true);
return $returnGoogleSearch["responseData"]["results"];
}
I found this second method for handling JSON.
/require_once(’JSON.phps’);
function retrieveGoogleSearch2($searchTerms=”hgh”,$searchURL, $start) {
$googleBaseUrl = “http://ajax.googleapis.com/ajax/services/search/web”;
$googleBaseQuery = “?start=$start&rsz=large&v=1.0&q=”;
$googleFullUrl = $googleBaseUrl . $googleBaseQuery . $searchTerms;
// $searchURL .
$handle = fopen($googleFullUrl, ‘rb’) or die(”Couldn t open” . $googleFullUrl);
$body = ”;
while (!feof($handle)) {
$body .= fread($handle, 8192);
}
fclose($handle);
$json = new Services_JSON();
$json = $json->decode($body);
return $json->responseData->results;
}
The search string must be encoded using %20 for instead of spaces between words.
Bookmark with:
Digg
reddit
Facebook
StumbleUpon