Location: PHPKode > scripts > Search results retriever > class.googleRetriever.php
<?php
/*
* Copyright (C) 2010 Pierre-Luc Germain (plger)
* See class.httpRetriever.php for more information.
*/

class googleRetriever extends httpRetriever {
	public $services = array('web','patent','books','images');
	public $allowed_params = array('service', 'q', 'v', 'rsz'	);
	public $wanted_fields = array('unescapedUrl', 'titleNoFormatting', 'content', 'visibleUrl');

	public function help(){
		echo '<pre> Sample usage:
$retriever = createHttpRequest("google");
$retriever->setParam("q", "my search terms");
$result_array = $retriever->fetch_results("array");
$result_count = $retriever->get_result_count();

 Other services (books, images, patent) :
$retriever = createHttpRequest("google");
$retriever->setParam("service", "books");
$retriever->setParam("q", "my search terms");
$result_array = $retriever->fetch_results("array");
$result_count = $retriever->get_result_count();
</pre>';		
	}
	
	public function setQueryParam($value){
		$this->params['q'] = $value;
	}
	
	public function standardize($array){
		$itemlist = array();
		foreach($array as $record){
			$newitem = array(	'title'=>$record['titleNoFormatting'],
								'link'=>$record['visibleUrl'],
								'author'=>isset($record['authors'])?$record['authors']:false,
								'date'=> isset($record['applicationDate'])?$record['applicationDate']:false;
								'year'=> (isset($record['publishedYear'])?$record['publishedYear']:(isset($record['applicationDate'])?substr($record['applicationDate'],0,4):false)),
								'number'=> $this->get_item_id($item)
							);
			array_push($itemlist, $newitem);
		}
		return $itemlist;
	}
	private function get_item_id($item){
		foreach(array('patentNumber', 'bookId', 'imageId') as $field)	if(isset($item[$field]))	return $item[$field];
		return false;
	}

	public function fetch_results($return='array'){
		$params = $this->params;
		$service = 'web';
		if(isset($params['service'])){
			if(in_array($params['service'], array('web','books','images','patent')))	$service = $params['service'];
			unset($params['service']);
		}
		$url = 'http://ajax.googleapis.com/ajax/services/search/'.$service.'?';
		$fields = array();
		switch($service){
			case 'patent':
				$resultClass = 'GpatentSearch';
				$fields = array('applicationDate', 'patentNumber', 'patentStatus', 'tbUrl');
				break;
			case 'books':
				$resultClass = 'GbookSearch';
				$fields = array('authors', 'bookId', 'publishedYear', 'tbUrl', 'pageCount');
				break;
			case 'images':
				$resultClass = 'GimageSearch';
				$fields = array('width','height','imageId','tbUrl');
			case 'web':
			default:
				$resultClass = 'GwebSearch';
				break;
		}
		$fields = array_merge($fields, $this->wanted_fields);
		
		$default_params = array( 'rsz' => 'large', 'v' => '1.0'	);
		$params = array_merge($default_params, $params);

		$url = $this->get_url_from_params($url, $params);
	
		$content = $this->retrieve_page($url);	
		if(!$content)	return false;

		$json = new Services_JSON();
		$json = $json->decode($content);

		if(!$json){
			$this->doDebug(true,'Could not decode JSON.', false, __FILE__,__LINE__);
			return false;
		}

		$itemlist = array();
		$this->resultCount = $json->responseData->cursor->estimatedResultCount;
		foreach($json->responseData->results as $searchresult){
			if($searchresult->GsearchResultClass == $resultClass){
				$item = array();
				foreach($fields as $field){
					$item[$field] = $searchresult->$field;
				}
				array_push($itemlist, $item);
			}
			
		}
		return $this->manage_return($itemlist, $return);
	
	}
	
}
Return current item: Search results retriever