How to Parse Yahoo! Search Results with a Developer API Key
As you may know, all major Search Engines provide API interface for developers. It is possible to use search results in your applications; there is a variety of languages that can be used to retrieve search results. I will show you a PHP solution that allows to get Yahoo! Search results using a short script.
We will need to have a developer key from Yahoo!. It can be obtained here. You will need to create a name for your script and register it. Once it’s done, you may proceed to the next stage: we will asome basic class that isavailable for free download.It’s PHP XML Library: quite old, but it does the job.
Once we have application ID and XML library we may start creating a script. I’ve used several functions I’ve found on the net but I don’t remember where I downloaded them from, so they’re not mine. Let’s declare these functions first:
/**
* Build an array with the parameters we want to use.
*
*/
function setParams($id, $site, $numresults, $keyword, $start) {
$params = array(
‘appid’ => $id,
‘site’ => $site,
‘query’ => $keyword,
‘results’ => $numresults,
‘start’=>$start
);
return $params;
}
This is quite a simple fuction that consolidates all the necessary parameters. The next one builds a query from these parameters.
/**
* A small url building function:
*
*/
function buildUrl($url, $params) {
$qstring = ”; // Initialize…
foreach($params as $key => $value) { // Build query string…
$qstring .= ‘&’.”$key=$value”;
}
$qstring = ltrim($qstring,’&’); // Trim the fat…
return $url.$qstring;
}
Another simple function that creates a string from an array with vatiables.
/**
* This function accesses an XML file ($url), reads it into a variable,
* and then unserializes it into an associative array:
*/function getXMLArray($url) {
$xml = ”;// Access the file ~ make the request:
$handle = fopen($url,”rb”);
while (!feof($handle)) {
// Read the resulting XML into the variabl $xml:
$xml .= fread($handle, 8192);
}
fclose($handle);// Unserialize the data:
$data = XML_unserialize($xml);
return $data;
}
And a special function for Yahoo!
/**
* This function formats our Yahoo! specific array:
*
*/function formatYahooResultset($data) {
// If no results were found:
if($data['ResultSet attr']['totalResultsReturned'] == 0) {
$results = ”;
return $results;
}foreach($data['ResultSet']['Result'] as $key => $value) {
$vydacha["title"][]=$data['ResultSet']['Result'][$key]['Title'];
$vydacha["url"][]=$data['ResultSet']['Result'][$key]['Url'];
$vydacha["summary"][]=htmlentities($data['ResultSet']['Result'][$key]['Summary']);
}
$results = $vydacha;return $results;
}
Done with functions. Let’s include our library.
// Include the library:
include(‘../lib/xml.php’);
We also need to specify some parameters: developer ID and keyword for search.
$id = ‘Developer ID’;
$keyword=”LAMP docs”;
You need to add your developer ID for a successful query. Here comes the main part of the script:
$site = ”;
$numresults = 100;
$start=0;$url = ‘http://api.search.yahoo.com/WebSearchService/V1/webSearch?’;
$params = setParams($id, $site, $numresults, $keyword, $start);
$url = buildUrl($url, $params);
$dateks = file_get_contents($url);
if (strlen($dateks)>0)
{
$data = getXMLArray($url);
$hopana = formatYahooResultset($data);
$hopana contains an array with urls, titles and descriptions. Perform a var_dump to find out its structure or take a look at formatYahooResultset function – it will tell you about the structure.