Working example of Microformats as an API

Drew posted a while ago asking can-your-website-be-your-api. The simple idea is that you just might be able to live without a dedicated API in favour of good use of microformats.

It also turns out Tantek has also been on the case with his presentation Can your website be your API? – Using semantic XHTML to show what you mean and

Glenn has spoken on the subject too at WebDD and BarCamp.

This has interested me for a while and I’ve finally gotten around to writing some code, in fact I wrote the bulk of it during The Highand Fling and then the rest before the Refresh event after I got to te venue an hour early.

I used Drew’s hKit for the microformat parsing so everything that follows is PHP5 only I’m afraid. The also means I dont have a working example on this server at the moment but you can grab the code and try it out yourself. It also makes use of a couple of PEAR modules; PEAR Validate and PEAR HTTP_Request.

At the moment I’ve worked on a simple hCard example. It demonstrates the idea of a website exposing methods based on the presense of microformats, plus using that exposed method to extract the information we’re looking for.

First we create objects for the parser and the website:

$parser = new hKit;
$website = new Website('http://morethanseven.net',$parser);

Then we’ll ask what methods the website exposes:

$website->expose();

In this example we get back an array of the available methods, in this case getContacts. Then just as an example lets print out some contact details:

foreach ($website->getContacts() as $contact) {
   echo $contact->getFN();
}

This is a very early release, more of a proof of concept and as such their is no documentation and only a portion of hCard is supported. Hell, their’s not even a name beginning with h! Having said that I have a plan for a little project to kick the tyres and so I’l l be adding to it and hopfully have a proper release at some point.

The plan is to introduce methods like getReviews, getEvents, etc. which allow for the extraction of the relevant details. The part I find most interesting however is the idea of the expose method – asking a web page if it has an API, and if it does then what information you can extract automagically. If you’re interested let me know what you think.

Download example

Comments

  1. Looks good Gareth. Turned out the reason it couldn’t parse my site at lunch was the YouTube player code.

    Adam - 7th April 2007

  2. Nice work Gareth, could prove very useful. I’ve had a crack extracting from LinkedIn which works, but only if info is public (may need to replace unencoded & etc). I would love to be wrong but doubt they will be providing an API anytime soon, backdoor API using microformats anyone?

    Steve Urmston - 10th April 2007

  3. Thanks. I’m increasingly of the mind that the use of microformats can replace the need for an API in lots of situations. Less a “backdoor” API than a more easily developed intentional one.

    gareth - 10th April 2007

  4. Looks like an interesting script and concept. Drew’s presentation last Thursday certainly got me thinking too (not least about all those funky black and white photographs “borrowed” from Flickr).

    I might try to play around with this script on the World Cup KickOff concept (and a private FA version).

    Si Jobling - 11th April 2007

Comments are now closed.