If you're new here, you may want to subscribe to my RSS feed. So that you can read the latest updates about Web2.0 tools, Making Money Online, Tips in SEO, Ajax and many more. Thanks for visiting ProgramimiCOM!

Most know about Google’s I’m Feeling Lucky feature, you enter a search term, and it automatically takes you to the first result. Yahoo has no such feature, but were going to make one. Yahoo’s great set of API’s, and the JSON PHP class makes this simple. We’re going to use the Yahoo! API for search requests and then decode the response with JSON, and redirect from there.

To begin, make sure you have a copy of the JSON class (linked above). Now, making the API request is simple, we form a URL and then get the contents. The content will be sent to JSON to be decoded, then sent back to use as a large array (and sub-arrays) and we’ll get the URL from there.

The request URL will look like this:

$request = ‘http://api.search.yahoo.com/WebSearchService/V1/webSearch?appid=SC&query=’. $_POST[‘query’] .‘&results=1&output=json’;

This sends a request to the web search service API with our posted query. (we also say to output and JSON and limit to 1 response) Next, we have to get the contents of the request and then load up JSON and decode:

$response = file_get_contents($request);
require_once(‘./json.class.php’);
$json = new Services_JSON();
$data = $json->decode($response);

Still simple. We get the contents of the request (which is just a jumble right now :P ) then require, start, and use the JSON class to decode our response. The data variable now contains a huge array, it’s interesting to see what happens if your print_r() it. If you do that, you might want to try to find the URL variable yourself, but if not, keep reading and I’ll show you. :)

$redirect = $data->ResultSet->Result[0]->Url;
header(“Location: $redirect”);

Really, it’s that simple. :) The URL has three parents, the actual result which contains the URL and other data about that particular result, the result set which is all the results, and the array itself.