Tuesday, September 22, 2009

REST Services Supporting XML and JSON Using Zend Framework

I have been working on a project to build a RESTful Web service to handle provisioning requests for a PHP web application.  The developer working on the web app is using Zend Framework web MVC.  I wanted to stick with PHP so I could "hand off" the provisioning API once it is done.  After doing some research, it seemed that Zend Framework also had good support for what I wanted to accomplish with REST.

I have never used Zend before, and my most recent PHP experience was over three years ago, so I was hoping there would be good examples/tutorials.  Fat chance.  I found several other blogs that gave an account of their own personal trials (and in a couple of cases, successes), but nothing in the order of "official".  To give credit, Chris Danielson's post was my starting point but left quite a bit out that I needed.


First, why if I specified "format=xml" in my URL did the view still get returned as JSON?  This was unnerving to say the least.  After doing some digging, I found the answer on the Zend Framework Manual.  Specifically,  the section titled "12.8.4.3.1. Default Contexts Available" mentions that you have to disable automatic JSON serialization.  Minor detail?  So finally my XML view start working when I ask them to...


Next, how the heck do I access the request body for a POST or PUT request?  In Chris Danielson's blog, his example shows the following to get an incoming parameter:
     $this->getRequest()->getParam($paramName)


This only works for the query string though!  So again, I did some looking, and found this:
     $this->getRequest()->getRawBody()


Makes sense...but again, would have been great to see an example of this somewhere.   All of the blogs I looked at focused on GET requests...as if POST is never used.  :-)


Once I had these issues ironed out, it was just a matter of implementation.  But as I got further into development, I saw a pattern forming that seemed ideal for the Framework to handle.  It occurred to me that - for each method handler - I was:
  • Adding an action handler to context switch for XML and JSON
  • Defining a common document structure for the response (view) to be returned, regardless of context.
  • Wrapping the view in "resolvable" phtml documents that encode the document in the proper context.
Why is all of this necessary?  So I asked the experts:  read my proposal for ZF-7825.  As of this posting, the proposal is still under development, but my guess is this will show up in some 2.x release of Zend Framework.

Overall I would say that Zend Framework is a good choice if you're starting a new Web service project with PHP.  It is under active development by numerous contributors and supports a large number of features out-of-the-box.


You can download the code here.


-aj

2 comments:

  1. What is the copyright and license of your code ?

    ReplyDelete
  2. I saw your answers on StackOverflow, Do you have any suggestions without using Zend?

    Thanks

    -Brad

    ReplyDelete