You may not know this, but all you need to get started using web services is a web browser. You might have never heard of web services, or assumed it’s too complicated, or maybe you tried it before but had a painful experience – it’s always possible for poor design or implementation to make simple things difficult.
Well, consuming half-decent web services isn’t necessarily difficult. Bear with me for the next 30 seconds of your life and I’ll show you some examples of easy, useful web services APIs.
It’s also probably worth noting that different browsers can behave differently, and I’ve done this stuff in Firefox 3. If the links below don’t work, copy-pasting the link into your browser address bar and hitting return should do the trick.
The Google Maps API
You can use the Google Maps API to turn a postcode into a latitude and longitude. Try it with this link.
http://maps.google.com/maps/geo?q=S51AB+uk&output=csv
Output in your browser:
200,5,53.4332097,-1.4436139
The first and second numbers are related to the query, the second two are the latitude and longitude of the UK postcode ‘S5 1AB’. You can change the postcode and country in your browser address bar – hit return to submit the new address details.
It’s a neat example, because it’s a very simple query and response.
Yahoo Finance
Now, let’s check the stock portfolio.
http://quote.yahoo.com/d/quotes.csv?s=MSFT&f=sl
The output of this one comes back as a Comma Separated Values document, same as the Google Maps example, but it is set up to save the values into a file instead of displaying it on your browser. If you open the file:
"MSFT",23.692,"8/14/2009","4:00pm",+0.072,23.62,23.80,23.51,46331960
So the stock price for Microsoft Corporation was 23.692 at Friday’s close.
Next up, twitter. If you’ve never heard of twitter, welcome to Earth. Let’s grab my public timeline.
http://twitter.com/statuses/user_timeline/brabster.xml
The output this time is a little more funky – because what we’ve just requested is much more complex than just a latitude and longitude. You’ll get an XML document back, which is a machine-friendly way of storing complex data. Not so nice for people to look at though, but hey.
Tue Sep 01 19:07:07 +0000 2009 3694475261 @cazm: hate that - if I don't know how come something's fixed I worry about when it's gonna break next! <a href="http://sourceforge.net/projects/twitux" rel="nofollow">twitux</a> false ...
Summary
Mixing and matching the functionality this stuff exposes creates mashups.
Whilst a browser provides a really easy way for us to explore these services, the trick is that it’s easy to write software that invokes services like these and then uses the information in the response, in fact in many languages invoking a service like those above and capturing the response can be done in a single short line of code.
Often, traditional websites that produce nicely human-readable, formatted pages like this one are actually working with APIs like those above behind the scenes.
There’s a whole world of web services APIs out there to play with, for free – and more are coming along all the time.
(Note – these examples will almost certainly break, given enough time, as the services evolve. If you try one and it doesn’t work, please leave me a comment and I’ll get it fixed.)