Sunday, October 16, 2005

I was playing around with the Yahoo Search API on the weekend and was very impressed with the wide range of services they offer up to developers.

I started looking around for a WSDL to use when I read that unlike MSN and Google, Yahoo does not use a web service, they use a method called Representational State Transfer or REST for short. "What the hell is REST? This is going to take forever!", I thought to myself. Well good thing REST is fairly easy to understand and pretty much any language can use it.

REST is defined in this Wiki entry as "any simple web-based interface that uses XML and HTTP without the extra abstractions of MEP-based approaches like the web services SOAP protocol." "What the hell does that mean?" you are probably thinking to yourself right now. Well to put it in simple terms, when you submit a URI like this http://api.search.yahoo.com/WebSearchService/V1/webSearch?query=steven+rockarts you can break your "restRequestuest" down into different parts.

Really you are saying, I want to use http://api.search.yahoo.com (the Yahoo Search API), /WebSearchService/V1/ (the web search service version 1) and webSearch?query=steven+rockarts (I want the resource you have for the web search part of the API that relates to the query Steven Rockarts (gratuitous plug)). That URL actually won't work because there needs to be an appId present try this:

http://api.search.yahoo.com/WebSearchService/V1/webSearch?query=steven+rockarts&appid=MY_ID

The Yahoo Search engine receives this request from you and says here is all the information I have for the query steven rockarts and i'm going to give it back to you in XML format.

So if you read the definition again it all should make sense. You are sending a GET via HTTP to Yahoo and they are sending you back XML. You can also use POST if you need to upload some data to the REST resource you are trying to access.

So now that you understand the dry boring part, here is some code that makes use of the Yahoo Search API. This example uses the Yahoo Search API and C#. (I have also provided the equivalent code in VB.NET).

Yahoo Search API C# Code:

YahooSearchConsole.zip (3.84 KB)

static void Main(string[] args)
{
            //Create a New Web Request to the API
            WebRequest restRequest;
            restRequest = WebRequest.Create("http://api.search.yahoo.com/WebSearchService/V1/webSearch?query=steven+rockarts&appid=MY_ID");
            restRequest.Method = "GET";

            //Submit synchronous HTTP restRequestuest to Web server
            WebResponse rsp = restRequest.GetResponse();

            //WebResponse provides stream-based access
            Stream str = rsp.GetResponseStream();
            StreamReader reader = new StreamReader(str);
            Console.WriteLine(reader.ReadToEnd());

            reader.Close();
            rsp.Close();
            Console.ReadLine();
}

Yahoo Search API VB.NET Code:

YahooSearchConsoleVB.zip (2.95 KB)

Sub Main()

'Create a New Web Request to the API
Dim restRequest As WebRequest
restRequest = WebRequest.Create("http://api.search.yahoo.com/WebSearchService/V1/webSearch?query=steven+rockarts&appid=MY_ID")
restRequest.Method = "GET"

'Submit synchronous HTTP restRequestuest to Web server
Dim rsp As WebResponse = restRequest.GetResponse

'WebResponse provides stream-based access
Dim str As Stream = rsp.GetResponseStream
Dim reader As New StreamReader(str)
Console.WriteLine(reader.ReadToEnd())

reader.Close()
rsp.Close()

Console.ReadLine()

End Sub

Theme design by Jelle Druyts

Pick a theme: