Weblogs: Atom

PHP AtomAPI Implementation

Wednesday, September 10, 2003

Part of the on-going argument against using REST to implement the AtomAPI is the workability of REST on commonly used architectures. With this in mind I wrote a very simplistic AtomAPI in Perl. Yet, I would prefer to do it in PHP.

PHP looked to be a stumbling block - however user-friendly the web-focused language seemed to hide the essential ingredients needed to write a REST web service using it. There seemed to be no way of getting to the actual XML string without having a server configuration of always_populate_raw_post_data set to On. This configuration is non-typical so people running on shared hosting would not be able to use PHP for Atom.

Arien to the rescue

Over on Sam Ruby's Pie wiki Arien provided the crucial piece of information I was looking for: php://input.

The input filter requires PHP 4.3.0 and above. For security reasons, webhosts - especially shared webhosts - should not be running a PHP version lower than this, so the audience lost from this particular requirement should not be significant.

My PHP Implementation

The entry URL to the PHP implementation is http://www.isolani.co.uk/atomblog/. POSTing an entry (or an entire Atom feed) will create blog entries using the entry title to create a shortname which is used as the blog URL. GETting the entry URL returns a feed of the last four elements (a summary feed without any content). This will provide the entry specific URL contained in each entry's link element.

The entry-specific URLs allow GET to retrieve the entry, PUT to update this entry, and DELETE to delete the blog entry.

This demonstrates PHP can handle the full range of HTTP verbs required by the AtomAPI.

Details of the implementation

I got a little carried away with the implementation and started experimenting with a SAX parser. I wanted to do something with the XML string, and as PHP's DOM implementation isn't widely available on shared hosting SAX was the only option.

The implementation takes the XML string and breaks it down into an array of entries. This allows complete atom feeds to be posted directly to the entry URL. These objects are then stored in three mySQL tables: atomentry, atomcontent and atomauthor. At the moment, contributors are ignored. There is no authentication.

The driving force behind this implementation is a proof-of-concept of PHP as a feasible option to write REST based web-services, and proof that AtomAPI is feasible on PHP.

The entire source code is available in a zip format. It uses an htaccess file to rewrite all URLs beginning with /atomblog/ to the /atomphp/atom.php. This is by no means an absolute requirement - using the PATH-INFO technique like the perl implementation is always an option. I prefer nice clean URLs, and mod_rewrite does that for me.

Terms and conditions of use

This implementation is in no means production ready or bug-free. The API it is based on is not formalised. There are bugs and security "features" with this code. Feel free to take the source code and play around with it - that's what I'll be doing. This is my first attempt at writing a webservice, so I've probably made some howlers, so don't treat this code as anything more than the random set of characters I extracted from /dev/null.

If you are writing a client and need a service to test it on, I'll volunteer this as long as you don't attempt to hack it or take advantage of my non-existant security. Also please don't hammer it. If I do run into a problem with resource or bandwidth issues, I'll probably have to take it down. Thanks.

Update 19 September: Just noticed a silly bug when updating entries, because of a non-existant where clause, all records are updated by doing a put on any single entry. This has been fixed and the new source (v0.0.2) made available.

[ Weblog | Categories and feeds | 2011 | 2010 | 2009 | 2008 | 2007 | 2006 | 2005 | 2004 | 2003 | 2002 ]