Yahoo recently released a new search API. Known as Yahoo BOSS, for “Build your Own Searcg Service,” the API allows you to query search results from their servers, format them however you want, mash the data up with other services, and even re-order results. You get “Unlimited*” queries (they just reserve “the right to limit unintended usage, such as automated querying by bots”) and they don’t even require attribution.
Even though I’m what you could possibly call a “Google/Apple fanboy” (though you would be advised to not say such things…), and I’ve long dismissed Yahoo as boring, geared towards web newbies, among other things, I have to admit, this is a great API. Google never gave us anything like this (despite their seemingly unlimited resources) and they discontinued their fairly limited search API. (As a side note, I also admit that Yahoo owns some great web services, such as Flickr and Del.icio.us.)
I’ve already got to work playing with the API, creating a sort of search mashup. I figured I’d share a little bit of code, and show you how to create a basic SERP. Be warned, the following requires PHP5 and some cURL black magic. (If you have no idea what I just said, read a book, and come back later.)
In order to use Yahoo BOSS, you need to get an API key. If you have a Yahoo account, it will only take about five minutes. Once you’ve got a key, you may want to take a quick look at the API documentation. There aren’t any functioning examples or tutorials, but the guide tells you what sort of queries you can send, and what you can expect to get back in response.
Now for the fun part. Here’s a basic example I threw together. You can use it as a starting point for your own project. It’s fairly basic, as it’s main goal is to illustrate how to go about requesting data from the api and displaying it. You should be able to run the code as-is, though you’ll need to insert your API key before running the script:
<?php if ($_GET['s'] == '') { ?>
<form method="get" style="margin-bottom:30px; margin-top:20px;">
<input type="text" name="s" size="30" /> <input type="submit" value="Search" />
</form>
<?php } ?>
<?php
if ($_GET['s'] != '') {
//Gather data and prepare query
$thequery = urlencode($_GET['s']);
$yhost = 'http://boss.yahooapis.com';
$apikey = 'PASTE_YOUR_API_KEY_HERE';
$url = $yhost.'/ysearch/web/v1/'.$thequery.'?appid='.$apikey.'&format=xml';
//Get the results
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
$results = new SimpleXmlElement($data, LIBXML_NOCDATA);
//echo the results
foreach ($results->resultset_web->result as $theresult) {
echo '<a href="'.$theresult->clickurl.'">'.$theresult->title.'</a><br/>';
echo $theresult->abstract.'<br/>';
echo '<small><i>'.$theresult->dispurl.'</i></small><br/>';
echo '<br/><br/>';
}
}
?>
As you can see, this is about as basic as an example as you can get. It shows the first ten web results for whatever you enter in the box. If you were to append “&start=20″ to the end of the query URL in the $url variable, it would show results 11-20 instead. That’s the beginning of creating a pagination system to allow a user to move beyond page one of the results.
If you have a little bit of PHP experience (like I recommended earlier; weren’t you listening?), the code should be fairly self-explanetory.
- First, the script outputs a search box, unless the form has already been submitted. If that’s the case, the script will move on two step 2.
- If someone has submitted the form, the value of the “s” variable from the browser URL’s query string (it looks like “?s=blah”) is reformatted so spaces and other special characters won’t make the BOSS API puke when it sees them.
- The API query is assembled. If you were to place the word “echo” before the “$url=…” statement you’d get to see the complete URL when the result page loads. It looks something like “http://boss.yahooapis.com/ysearch/web/v1/iphone?appid=YOUR_API_KEY&format=xml”, where iPhone is the search query and YOUR_API_KEY is, well, your API key. If you’re a JSON maniac you can replace format=xml with format=json, but if you want to do that, you probably don’t need my tutorial.
- Next, we use the useful cURL extension to send a request to Yahoo saying “give us the data, Yahoo.” After it gets a response, the
SimpleXmlElement
line parses the returned XML file into a PHP Object. - Now we move onto the part where we display the results with a simple
foreach()
loop. You can style it however you want, maybe write some more semantically correct XHTML, but good old <br /> works well enough to illustrate my point. (Yeah, I’m lazy. )
Now make it your own. You’ve got a basic starting point, and hopefully some knowledge on how it works. Do something unique with it, mash the data up with data from another API.
EDIT: Some of you have been having trouble with the code above. WordPress seems to be trying to “fix” parts of it. Instead of copy/pasting the code above, you should download this .txt file.
Pingback: Sikbox - Live Search Made Easy | Webmaster-Source
Pingback: Yahoo Boss PHP code examples are hard to come by : SubscribeToMyBlog.com