Reputation: 21
I want to complement my webservice with a list of results provided by Google Search. My target is to receive the first 10-20 results by specifying some keywords and some other information (data range for example).
What possibilities do I have? Can this even be realised only by using client-side technology like Javascript? Or do I eventually have to rely on a dynamically loaded PHP script which loads and analyses the HTML code by using cURL?
Upvotes: 2
Views: 8141
Reputation: 4411
This is how I've done it in the past with a cURL request and the Google AJAX API:
function getResults($term,$page = 0){
$googleurl = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=".(str_replace(' ','+',$term);
if($page >0){
$googleurl .= "&start=".($page*10);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$googleurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$Page_Contents = curl_exec($ch);
curl_close($ch);
return $Page_Contents;
}
$keywordCheck = array('ajax','php','cURL','other search terms');
foreach($keywordCheck as $wordKey => $wordValue){
$term = str_replace(' ','+',$wordValue);
for($i =0; $i < 5; $i++){
$page = getResults($term,$i);
if(preg_match("/mysite.com/",$page)){
$links = explode('GsearchResultClass',$page);
foreach ($links as $key => $value){
if(preg_match("/mysite.com/",$value)){
preg_match("/url\":\"([^\"]*)\",/",$value,$match);
$match = str_replace('",','',str_replace('url":"','',$match[0]));
$results[$wordValue] .= $wordValue. ' is ranked #'.(($i*10)+($key)).': '.$match.'<br/>';
}
}
}
}
sleep(2);
}
then in in the page body:
<div id="results">
<?php foreach($results as $key => $value){
if($value == ''){
echo '<p>'.$key.' not found in top 50 results</p>';
}
else{
echo '<p>'.$value.'</p>';
}
}?>
</div>
Some points to understand:
1) Use sleep() to pause between queries to avoid getting banned for overburdening the server
2) Last time I checked, the search api limits you to 1000 queries per day, so plan accordingly
3) The API queries a different database than the www.google.com, so results might be slightly different then what you see when you do a normal search
This script just checks the position(s) of my urls on certain keywords I want to monitor the position of within the first 50 results... obviously you would need to make adjustments if you were not focused on 1 site...
to add custom filters, just do an "advanced" search and look at the query string, its the same for the API. Just append as needed, like for a date range of Jan 1 2011 - Jan 24 2011:
$googleurl .= '&tbs=cdr%3A1%2Ccd_min%3A1%2F4%2F2011%2Ccd_max%3A1%2F24%2F2011';
Upvotes: 1
Reputation: 27057
You want to check out their JSON search API (which is a part of their larger custom search API). The example on that page is in javascript, but you could just as easily implement that in php.
Upvotes: 1