From WorldCat Developers' Network
PHP Function for WorldCat API OpenSearch Requests
Description
Given OpenSearch parameter values and a valid OCLC web service key, formats a WorldCat API OpenSearch request URL, gets a request response, transforms the response into a PHP Array, and returns the array.
This function assumes the use of a PHP RSS parser, in this instance the MagpieRSS parser (http://magpierss.sourceforge.net/).
worldcat_opensearch( string $key, string $query, string $format, string $start, string $count, string $cformat);
Parameters
- $key = The WorldCat Search API key assigned by OCLC
- $query = Search query terms
- $format = rss or atom (default)
- $start = Starting record position
- $count = Maximum number of records per response (no more than 50)
- $cformat = The preferred citation format: apa, chicago, harvard, mla, or turabian
Return Values
This function returns null (for zero results and any request errors) or an array of one or more records, each record represented by an array with these values:
- title = The item title
- author = The author name, if any
- summary = A summary of the item, if available
- link = A link to the item in WorldCat.org
- content = An encoded HTML string representing the item in the specified citation format
Function Code
// Require MagpieRSS, change the path to its location on your server
// For more information on MagpieRSS: http://magpierss.sourceforge.net/
require_once('rss_fetch.inc');
function worldcat_opensearch($key,$query,$format,$start,$count,$cformat) {
// declare array
$r = null;
// set counter
$c = 0;
// construct worldcat opensearch request
$url = "http://www.worldcat.org/webservices/catalog/search/worldcat/";
$url .= "opensearch?q=";
$url .= urlencode($query);
$url .= "&format=".$format;
$url .= "&start=".$start;
$url .= "&count=".$count;
$url .= "&cformat=".$cformat;
$url .= "&wskey=".$key;
// fetch the OpenSearch result
$rss = fetch_rss($url);
// if there was a result
if ($rss) {
// for each item
foreach ($rss->items as $item) {
// get descriptive data
$title = $item['title'];
$author = $item['author_name'];
if ($format == "rss") {
$summary = $item['description'];
$link = $item['link'];
$content = $item['content']['encoded'];
} else {
$summary = $item['summary'];
$link = $item['id'];
$content = $item['atom_content'];
}
// add item values to an array
$a=array("author"=>$author,"title"=>$title,"summary"=>$summary,"link"=>$link,"content"=>$content);
// add the item to the result array
$r[$c] = $a;
// increment the result counter
$c++;
} // end foreach item
} // end if rss
echo "</ul>";
// return the result array
return $r;
} // end worldcat_opensearch
Examples
To submit a request for the search terms stored in $query, with a response in the Atom publishing format, starting with record 1, showing a maximum of 5 records, and with citations in the MLA format (the API key is assumed to be stored in the $key variable, in this example):
if (strlen($query) > 0) {
$worldcat = worldcat_opensearch($key,$query,"atom","1","5","mla");
if ($worldcat) {
foreach ($worldcat as $r) {
echo "Citation: ".$r["content"];
echo "Author: ".$r["author"]."<br/>";
echo "Title: ".$r["title"]."<br/>";
echo "Summary: ".$r["summary"]."<br/>";
echo "<a href=\"".$r["link"]."\" target=\"_blank\">Find in a library</a>";
echo "<hr/>";
}
}
}
