W3C home > Mailing lists > Public > public-lod@w3.org > September 2012

njh.me - content negotiation in PHP

From: Nicholas Humfrey <nicholas.humfrey@bbc.co.uk>
Date: Sun, 9 Sep 2012 11:46:10 +0100
To: <public-lod@w3.org>
Message-ID: <CC723202.80B64%nicholas.humfrey@bbc.co.uk>

Apologies for a bit a shameless self-promotion but I think it might be of
interest to this group.

I was recently looking at my WebID:

And realised that I could have this instead and save a few bytes ;-)

So I bought njh.me and I am now using it as the identifier for me, while
keeping my homepage at http://www.aelius.com/njh/

Doing content negotiation properly is still badly supported by most
languages and environments. I wanted to use a lightweight MVC framework and
chose Slim. Slim has had content negotiation on its backlog for quite a long
time but I decided that if I wanted to get it working any time soon, I was
going to have to implement it myself.

My fork is here:

And there is a pull request to bring it into master here:

The negotiation API is very simple and allows you to do this:
$format = $app->respondTo('html', 'rdf', 'ttl', 'json');

The list of suffixes is the file formats that are supported by the
application. They are ordered by the server's preference of format. The
client can either use an Accept header to select the desired format or force
a specific format by appending a suffix to the URL, for example:


If you make a request with no Accept header and no suffix, then the first
format in the list will be returned (HTML). The client can use q values to
rank its preferred format.

The Slim application for njh.me is only 32 lines long and it hopefully very
easy to read:

It depends upon Composer, Slim and EasyRdf. There are some PHPUnit tests to
ensure the it behaves correctly.

Web browsers requesting http://njh.me/ will be redirected, using a 303 to my
homepage. A Semweb clients setting a Accept: application/rdf+xml will be
sent a 303 redirect to http://njh.me/foaf.rdf, which in turn will return

Hopefully this is a pattern that people can re-use and apply to more complex


This e-mail (and any attachments) is confidential and may contain personal views which are not the views of the BBC unless specifically stated.
If you have received it in error, please delete it from your system.
Do not use, copy or disclose the information in any way nor act in reliance on it and notify the sender immediately.
Please note that the BBC monitors e-mails sent or received.
Further communication will signify your consent to this.
Received on Sunday, 9 September 2012 10:46:42 UTC

This archive was generated by hypermail 2.4.0 : Thursday, 24 March 2022 20:30:01 UTC