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

class jstorRetriever extends httpRetriever {
	
	public $allowed_params = array(	'operation', 'maximumRecords', 'query', 'title'	);
	public $wanted_fields = array('dc:date','dc:relation','dc:publisher','dc:coverage','dc:title');

	public function help(){
		echo '<pre>Sample usage:
$retriever = createHttpRequest("jstor");
$retriever->setParam("query", "philosophy");
$retriever->setParam("maximumRecords", 20);
$result_array = $retriever->fetch_results("array");
</pre>';		
	}

	public function setQueryParam($value){
		$this->params['query'] = $value;
	}
	
	public function standardize($array){
		$itemlist = array();
		foreach($array as $record){
			$newitem = array(	'title'=>$record['title'],
								'author'=>$record['author'],
								'date'=>$record['date'],
								'year'=>$record['year'],
								'number'=>false,
								'link'=>false
							);
			array_push($itemlist, $newitem);
		}
		return $itemlist;
	}	

	public function fetch_results($return='array'){
			
		$params = $this->params;
		
		$parameters = array( 'operation'=>'searchRetrieve', 'maximumRecords' => '20', 'query'=>'' );

		if(isset($params['query']))	$params['query'] = urlencode(urldecode($params['query']));
		if(isset($params['title') && $params['title'] != '')	$params['query'] = "dc.title+%3D".urlencode(urldecode($params['title']));
		unset($params['title']);
		
		$parameters = array_merge($parameters, $params);
		
		$url = "http://dfr.jstor.org/sru/?version=1.1"
		foreach($parameters as $key=>$value)	$url .= "&".$key."=".$value;
		
		$content = $this->retrieve_page($url);	
		if(!$content)	return false;

		$xml = new DOMDocument();
		if(!($xml->loadXML($content))){
			$this->doDebug(true,'Could not parse xml.', false, __FILE__,__LINE__);
			return false;			
		}
	
		$records = $xml->getElementsByTagName('record');
		$itemlist = array();
		$wanted_fields = $this->wanted_fields;
		foreach( $records as $record ){
			$item = array('publisher'=>'', 'authors'=>array());
			$children = $record->getElementsByTagName('*');
			foreach($children as $node){
				if($node->nodeName == 'dc:creator'){
					array_push($item['authors'], $node->nodeValue);
				}elseif(in_array($node->nodeName, $wanted_fields)){
					$clean_name = str_replace('dc:','',$node->nodeName);
					$item[$clean_name] = $node->nodeValue;
				}
			}
			$item['author'] = implode(', ',$item['authors']);
			$item['year'] = (isset($item['date']))?str_replace('YEAR: ','',$item['date']):false;
			array_push($itemlist, $item);
		}		
		
		return $this->manage_return($itemlist, $return);
	
	}
	
}
Return current item: Search results retriever