Bright Hub
 
Matthew Casperson's Hubfolio

Yahoo BOSS Custom Search with PHP

Article by Matthew Casperson (4,883 pts )
Published on Nov 2, 2009

This free tutorial shows PHP developers how to take advantage of Yahoo's search technology to build a quick, effective custom search solution.

Introduction

DOWNLOAD THE SOURCE CODE

Site searches have traditionally been quite poor. Google, Yahoo and other search providers have spoiled web users with their effective, targeted search results, and this level of convenience is something that internal site searches have not loved up to.

Unfortunately for web site owners, users will simply drop back to Google or Yahoo when their custom search fails, and this could lead them to competitors sites.

But thanks to Yahoo Build Your Own Search Solution (BOSS), web developers can now make use of Yahoo's cutting edge search platform to build their own custom search solutions. And best of all, it can be done with only a few lines of code.

Yahoo BOSS PHP Screenshot

Step 1 - Get a Yahoo API Key

In order to access the Yahoo search service, you will need an API key. These can be freely obtained from here.

Step 2 - Create the REST URL

Access to the Yahoo search service is done via a REST API. In order to access it the URL needs to be created.

$yahooAPIKey = "YourAPIKeyHere";

$baseYayooURL = "http://boss.yahooapis.com/ysearch/web/v1/";

$appid = "?appid=" . $yahooAPIKey;

$format = "&format=json";

$domain = "&sites=brighthub.com";

$query = urlencode("Flex Tutorials");

$fullURL = $baseYayooURL . $query . $appid . $format . $domain;

This code breaks down the URL into its individual components. The end result of this is a URL that looks like:

http://boss.yahooapis.com/ysearch/web/v1/Flex+Tutorials?appid=YourAPIKey&format=json&sites=brighthub.com

For a complete breakdown of the parameters that can be supplied, see this webpage here.

Step 3 - Call the service

Next we use CURL to call the URL defined in step 2. This will save the JSON object text in the variable called $resultsJson.

$session = curl_init($fullURL);

curl_setopt($session, CURLOPT_HEADER, false);

curl_setopt($session, CURLOPT_RETURNTRANSFER, true);

$resultsJson = curl_exec($session);

curl_close($session);

Step 4 - Decode the JSON

PHP has native support for converting JSON objects to PHP objects. This is done by calling json_decode.

$results = json_decode($resultsJson);

Step 5 - Display the results

Now that we have a PHP object with the results from the Yahoo search, we need to display them. How you display the results is really defined by the style of the page. Here we have simply displayed the title and abstract in a fairly crude list, but this should give you an idea of what can be done with the results.

foreach ($results->{'ysearchresponse'}->{'resultset_web'} as $result)

{

echo "<h3><a href=\"{$result->{'clickurl'}}\">{$result->{'title'}}</a></h3>\n";

echo "<p>{$result->{'abstract'}}</p>\n";

}

Step 6 - Other details

I have included a cut down example of the returned JSON object. Here you can see the properties that are returned for each search result. I highly recommend using an online JSON parser, like the one here, to view the results of a JSON web call.

{

"ysearchresponse": {

"responsecode": "200",

"nextpage": "\/ysearch\/web\/v1\/Flex%20Tutorials?format=json&count=10&appid=YourAPIKey&start=10",

"totalhits": "434056",

"deephits": "12900000",

"count": "10",

"start": "0",

"resultset_web": [

{

"abstract": "FlexTu torial.org is about using Adobe Flex<\/b> to develop Rich Internet Application (RIA). Here you will find a lot of tutorials<\/b>, tips, and code examples. Have fun and ...<\/b>",

"clickurl": "http:\/\/lrd.yahooapis.com\/_ylc=BlahBlah=10t0l4nrm\/**http%3A\/\/flextutorial.org\/",

"date": "2009\/10\/21",

"dispurl": "flextutorial.org<\/b>",

"size": "50885",

"title": "Rich Internet Application Development by Adobe Flex<\/b> - Flex<\/b> ...<\/b>",

"url": "http:\/\/flextutorial.org\/"

}

]

}

}

Yahoo BOSS PHP Screenshot

Conclusion

There are very few restrictions on what you can do with Yahoo BOSS. Yahoo have stated that they don't want to repeat past mistakes that have lead to search services, like BOSS, being too restrictive and ultimately unpopular with 3rd party web developers. So with just a few lines of code you can have your very own Yahoo powered site search, and then display the results any way you want.

Back to the Tutorial Index

Related Article

lastfmCreate a gig guide with Adobe Flex and Last.FM

This tutorial will step you through the process of creating a local gig guide with Last.FM and Adobe Flex.

Search More About:

 
Follow Matthew Casperson
Receive weekly updates from Matthew Casperson
 
Bright Hub - Science & Technology Articles, Buyer's Guides, How-To Tips and Software Reviews
About Bright Hub | Contact Us | Advertise with Us | Become a Writer | RSS | Site Map | Terms of Use | Privacy Policy | Copyright Policy
©2009 Bright Hub Inc. All rights reserved. Page copy protected against web site content infringement by Copyscape