Tag Archives: xml

XML to JSON in PHP

Zimuel writes; Last friday, in occasion of the April Zend Framework Bug-Hunt, I started to look at this bug: ZF-3257. This is an issue related to the Zend_Json class that occurs during the conversion from XML to JSON for some specific XML documents, like this one:

$xml= '<a><b id="foo">bar</b></a>';

The result using Zend_Json::fromXml($xml, false) , where false indicated the usage of XML attributes, was:

{"a":{"b":{"@attributes":{"id":"foo"}}}}

As you can see the bar value, of the a element, is not represented in JSON. This issue comes also with other XML documents, and in general when an XML node has a single character data child, any attributes are lost.

For instance, the following code:

$xml = '<a><bid ="foo">bar</bid></a>';
echo Zend_Json::fromXml($xml, false);

Produced the output:

{"a":{"b":"bar"}}

in this case the attribute id and the value foo are lost.
Find a solution @ Zimuel’s blog.

App_Controller_Helper_Params for JSON and XML POSTs

Matthew Weier O’Phinney shares a bit of very useful code to inject request params into a Zend Framework request object from a JSON or XML POST request.

“Below is a plugin I use to translate JSON or XML raw post request data to request user parameters.
Note that it expects a “Content-Type” header of either “application/json” or “application/xml”. If those are detected, it then does the translation and injection.
Once it has, you can then simply access the parameters from your request object like any others.
I actually use this with dojox.data.JsonRestStore already, so you should be set. :)”

<?php
class App_Controller_Helper_Params
    extends Zend_Controller_Action_Helper_Abstract
{
    /**
     * @var array Parameters detected in raw content body
     */
    protected $_bodyParams = array();

    /**
     * Do detection of content type, and retrieve parameters from raw body if
     * present
     *
     * @return void
     */
    public function init()
    {
        $request     = $this->getRequest();
        $contentType = $request->getHeader('Content-Type');
        $rawBody     = $request->getRawBody();
        if (!$rawBody) {
            return;
        }
        switch (true) {
            case (strstr($contentType, 'application/json')):
                $this->setBodyParams(Zend_Json::decode($rawBody));
                break;
            case (strstr($contentType, 'application/xml')):
                $config = new Zend_Config_Xml($rawBody);
                $this->setBodyParams($config->toArray());
                break;
            default:
                if ($request->isPut()) {
                    parse_str($rawBody, $params);
                    $this->setBodyParams($params);
                }
                break;
        }
    }

    /**
     * Set body params
     *
     * @param  array $params
     * @return Scrummer_Controller_Action
     */
    public function setBodyParams(array $params)
    {
        $this->_bodyParams = $params;
        return $this;
    }

    /**
     * Retrieve body parameters
     *
     * @return array
     */
    public function getBodyParams()
    {
        return $this->_bodyParams;
    }

    /**
     * Get body parameter
     *
     * @param  string $name
     * @return mixed
     */
    public function getBodyParam($name)
    {
        if ($this->hasBodyParam($name)) {
            return $this->_bodyParams[$name];
        }
        return null;
    }

    /**
     * Is the given body parameter set?
     *
     * @param  string $name
     * @return bool
     */
    public function hasBodyParam($name)
    {
        if (isset($this->_bodyParams[$name])) {
            return true;
        }
        return false;
    }

    /**
     * Do we have any body parameters?
     *
     * @return bool
     */
    public function hasBodyParams()
    {
        if (!empty($this->_bodyParams)) {
            return true;
        }
        return false;
    }

    /**
     * Get submit parameters
     *
     * @return array
     */
    public function getSubmitParams()
    {
        if ($this->hasBodyParams()) {
            return $this->getBodyParams();
        }
        return $this->getRequest()->getPost();
    }

    public function direct()
    {
        return $this->getSubmitParams();
    }
}

Converting your Zend Framework MVC application into an XML webservice using one single plugin

Thijs Feryn writes an excellent article on how to convert your entire MVC app or one or more controllers into a XML service. I actually have this one in a production environment and it works like a charm.

That’s right folks, in this blog post I’ll show you how you can convert your entire MVC application into a REST-style XML webservice. And I’m not talking about refactoring tons of code … NO, we’ll plug this option in without changing a single thing to your action controllers.

This post will contain a detailed description of the concepts used. The source could of the plugin is also shown and finally how it will look like when using it.

The concept

Thanks to those lovely hooks in Zend Framework you can simply intervene in nearly every aspect of the MVC workflow. The image below shows you the workflow.

Zend Controller Basics

Zend Controller workflow The hooks I’m talking about are actually just methods that are called by the plugin broker system. They already exist as empty methods in the Zend_Controller_Plugin_Abstract which we inherit from. So we override them in order to hook into the MVC flow.

Alle information on Zend Framework plugins can be found in the reference pages. A lot of you Zend Framework experts will now say: “why don’t you just use the context switching action helper?”. I could have done that, but this requires modifying your application. This post is about plugging it in, remember?

How am I doing it then? Well … I hook into the workflow at “post dispatch” time, this means that de dispatcher has already processed al controllers and is about to send all about back to the browser by using a Zend Controller Response object.

Before the front controller sends the output back to the browser, we empty the output’s body and add our own content. This content is retrieved from the view object which would normally parse and render the view object. The view has a set of properties which are set by the controllers. I use the reflection API to get a hold of all these items. Finally I serialize them and output it all.

The plugin

&lt;?php
/**
 * My_Plugin_Xml component
 * Turns an Zend Framework MVC website into an XML webservice
 */
/**
 * My_Plugin_Xml class
 *
 * @author Thijs Feryn &lt;[email protected]&gt;
 */
class My_Plugin_Xml extends Zend_Controller_Plugin_Abstract
{
    /**
     * Stores the front controller
     *
     * @var Zend_Controller_Front
     */
    private $_front;
    /**
     * Stores the XML output in DOMDocument format
     *
     * @var DOMDocument
     */
    private $_xml;
    /**
     * Class constructor
     */
    public function __construct()
    {
        $this-&gt;_front = Zend_Controller_Front::getInstance();
        $layout = Zend_Layout::getMvcInstance();
        $layout-&gt;disableLayout();
    }
    /**
     * Build DOMDocument to convert output to XML
     *
     * @param mixed $return
     * @param Exception $exception
     * @return string
     */
    private function _getXML($return = null,Exception $exception = null)
    {  
        $this-&gt;_xml = new DOMDocument('1.0', 'UTF-8');
        $this-&gt;_xml-&gt;formatOutput = true;
 
        $responseNode = $this-&gt;_xml-&gt;createElement('response');
 
        $exceptionNode = $this-&gt;_xml-&gt;createElement('exception');
        if(null !== $exception && $exception == instanceof( Exception ){
            $exceptionNode-&gt;appendChild(
                $this-&gt;_xml-&gt;createElement('message',
                    $exception-&gt;getMessage()
                )
            );
            $exceptionNode-&gt;appendChild(
                $this-&gt;_xml-&gt;createElement('code',
                    $exception-&gt;getCode()
                )
            ); 
            $exceptionNode-&gt;appendChild(
                $this-&gt;_xml-&gt;createElement('type',
                    get_class($exception)
                )
            ); 
        }
 
        $responseNode-&gt;appendChild($exceptionNode);
        if(null !== $return){
            $responseNode-&gt;appendChild(
                $this-&gt;_serialize('return',$return)
            );
        } else {
            $responseNode-&gt;appendChild(
                $this-&gt;_xml-&gt;createElement('return')
            );
        }
 
        $this-&gt;_xml-&gt;appendChild($responseNode);
        return $this-&gt;_xml-&gt;saveXML();
    }
    /**
     * Modify the HTTP response object
     * Remove the HTML body, replace with XML and change the content-type
     *
     * @param mixed $return
     * @param Exception $exception
     */
    private function _setResponse($return = false,Exception $exception = null)
    {
        $this-&gt;getResponse()-&gt;setHeader('Content-Type','text/xml; charset=UTF-8');
        $this-&gt;getResponse()-&gt;clearBody();
        $this-&gt;getResponse()-&gt;setBody(
            $this-&gt;_getXML($return,$exception)
        );         
    }
    /**
     * Serialize a mixed value to XML in DOMElement format
     * This method can be used recursively in case of objects and arrays
     *  
     * @param string $name
     * @param mixed $value
     * @return DOMElement
     */
    private function _serialize($name,$value)
    {
        if(is_array($value)){
            $element = $this-&gt;_xml-&gt;createElement($name);
            foreach ($value as $k=&gt;$v){
                if(is_numeric($k)){
                    $k = 'item';
                }
                $element-&gt;appendChild($this-&gt;_serialize($k,$v));
            }
        } elseif(is_object($value)){
            $element = $this-&gt;_xml-&gt;createElement($name);
            $reflection = new ReflectionObject($value);
            $properties = $reflection-&gt;getProperties();
            foreach ($properties as $property){
                if($property-&gt;isPublic()){
                    $element-&gt;appendChild(
                        $this-&gt;_serialize(
                            $property-&gt;getName(),
                            $property-&gt;getValue($value)
                        )
                    );
                }
            }
        }else{
            $element = $this-&gt;_xml-&gt;createElement(
                $name,
                (string)$value
            );
        }
        return $element;
    }
    /**
     * preDispatch hook that retrieves if an Exception was thrown in the application
     * If an exception is thrown, the exception is passed to the exception part of the XML output and script execution is terminated
     *
     * @param Zend_Controller_Request_Abstract $request
     */
    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        if($this-&gt;getResponse()-&gt;isException()){
            $exArray = $this-&gt;getResponse()-&gt;getException();
            $this-&gt;_setResponse(null,$exArray[0]);  
            $this-&gt;getResponse()-&gt;sendResponse();
            exit();
        }
    }
    /**
     * postDispatch hook that serializes the view object to XML by modifying the HTTP response
     * If no exception was thrown script execution continues and the postDispatch method will be called
     *
     * @param Zend_Controller_Request_Abstract $request
     */
    public function postDispatch(Zend_Controller_Request_Abstract $request)
    {
        $view = Zend_Controller_Action_HelperBroker::getExistingHelper('ViewRenderer')-&gt;view;
        $this-&gt;_setResponse($view); 
    }
}

The plugin registration

To activate the plugin, just call the registerPlugin method from the front controller. In my case this is done in my Initializer class which is allready a plugin.

$this-&gt;_front-&gt;registerPlugin(new My_Plugin_Xml());

The app

This application has one single controller which is the IndexController. There are 2 actions:

* IndexAction: the default action which assigns an object to the view
* ExceptionAction: an action which throws an exception

Pretty simple, pretty basic, but mind the exception: my plugin can catch it by hooking into the flow at preDispatch time. At that time we can already determine if the response contains an exception. Luckily, in this stage the front controller hasn’t yet sent additional error output to the view.

&lt;?php
class IndexController extends Zend_Controller_Action
{
    public function indexAction()
    {
        $obj = new stdClass();
        $obj2 = new stdClass();
        $obj2-&gt;c = 'xyz';
        $obj2-&gt;d = '123';
        $obj-&gt;a = 'abc';
        $obj-&gt;b = $obj2;
        $this-&gt;view-&gt;content= $obj;
    }
    public function exceptionAction()
    {
        throw new Exception('It all goes wrong!');
    }
}

The output

indexAction

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;response&gt;
  &lt;exception/&gt;
  &lt;return&gt;
    &lt;content&gt;
      &lt;a&gt;abc&lt;/a&gt;
      &lt;b&gt;
        &lt;c&gt;xyz&lt;/c&gt;
        &lt;d&gt;123&lt;/d&gt;
      &lt;/b&gt;
    &lt;/content&gt;
  &lt;/return&gt;
&lt;/response&gt;

exceptionAction

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;response&gt;
  &lt;exception&gt;
    &lt;message&gt;It all goes wrong!&lt;/message&gt;
    &lt;code&gt;0&lt;/code&gt;
    &lt;type&gt;Exception&lt;/type&gt;
  &lt;/exception&gt;
  &lt;return/&gt;
&lt;/response&gt;