Web Service Based Web Sites

Monday, June 20, 2005

Web Services (ws) and Web Sites have many things in common. So, why not put them together? I gave it the classic "Jared try" to find out if I could. (http://blueblocks.lobberecht.com)

DB <-> Business Logic <-> Web Services <-> Web Browser

First, I had to make some allowances for the Web Browser. No browser, that I know of, will directly communicate with a ws. Browsers were designed to use pure HTTP. My solution was to create a PHP file to act as a ws proxy for the browser.
DB <-> Business Logic <-> Web Services <-> PHP proxy <-> Web Browser
(I used PHP because my host only supports PHP. You could accomplish this in any server-side language, perhaps even a client-side language, that has a ws library. I also used PHP for the SOAP webservices.)

I chose not to output HTML from my ws. HTML was designed to describe the look-and-feel of data, not content. My ws outputs standard XML, conforming to various specifications, based on the content (RSS for the News, etc.), to maintain it's programmatic accessibility and compliance to accepted standards. I still had to contend with the browser, because it is still a web site, so, I used XSLT to transform the XML into XHTML on the client side. The client side XSLT transformation does limit my audience to modern webbrowsers, such as IE6 and Firefox, but I'm not trying to make this site universally compatible. Besides, you should be running a modern browser for security reasons anyway.

None. There are no advantages, in this design, over having an HTML page and a separate XML page, expressing the same content, generated directly from the same datasource. I have, simply, made the conversion process, from data to HTML formatted response, serial. (DB->XML->HTML) Some would also consider this a weak point in the design, because the HTML is dependent on the XML and the DB instead of just the DB.
This might be a good design if the site is intended to be used programmaticly, with a fall-back to HTML for those who wish to use it that way.

A little more difficult to code, especially if you are already using a CMS or other HTML based template engines.

Anyone returning XML from a Web Service should remember, the XML validator that validates the ws response will strictly validate your returned XML also. Make sure the returned XML is valid, has the <?xml version="1.0"?> tag, and a DOCTYPE or the whole ws response will not validate. You can also use a CDATA tag to enclose the response, but that did not work for me.