I Love Me Some APIs

I love APIs. They make using web services so much easier.

I’ve been developing an IRC bot as a side project, and three of its functions require the use of web services. It can shorten urls using http://is.gd/, search google, and validate web pages using the W3C’s HTML and CSS validators. Fortunately, and to my great elation, all four services have APIs available.

Is.gd’s API is straightforward and easy to use: call the page http://is.gd/api.php?longurl=http://really-long-url-goes-here.com , and is.gd will return the shortened url (or an error message) as the only text. You’ll have to parse out the HTTP headers, of course, but that’s the easy part.

The W3C’s APIs are just as easy to use. For HTML validation, fetch the page http://validator.w3.org/check?uri=http://webpage-to-check.com . This is the same page you would navigate to in a browser, and returns a full-on HTML page with the results. However, it also puts custom headers in the HTTP response. To get the results, you have to parse out the values of the X-W3C-Validator-Status, X-W3C-Validator-Errors, and X-W3C-Validator-Warnings headers. For CSS, the same info comes in the headers of a call to http://jigsaw.w3.org/css-validator/validator?uri=http://webpage-to-check.com . In the case of the CSS validator, though, the X-W3C-Validator-Warnings header is not returned.

Google’s search API is a little more involved. Not only because it uses JSON, but because it’s a secret. Google’s AJAX Search package is no secret of course, but the API calls that it makes are hidden away. Fortunately, Google set up an experimental search site called SearchMash.com, along with an API of its own. Simply fetch the page http://www.searchmash.com/results/search+terms+go+here , and searchmash will return a JSON string which can then be parsed out to obtain the google search results, alternate spelling suggestions, estimated number of results, the works. Even better, you can append “?n=X” to the end to limit the search to X results. Using ?n=1 returns the top result only.

These APIs all save processing power that would otherwise be spent parsing out possibly badly-formed HTML from HTTP responses.

Update: The SearchMash API has “gone the way of the dinosaur.” If you’re looking for something similar to what SM offered, check out Google’s RESTful search API: AJAX API for non-JS environments. It gives the same results in basically the same format — I was able to switch over with very few adjustments to my own code.

Aside: If you would like to check the bot out, it currently resides on #ecode (and, probably, other channels) on the freenode network. It goes by the name of “ecode”.

Leave a Comment

You need to enable javascript in order to use Simple CAPTCHA.