-- OakBehringer <[email protected]> wrote
(on Wednesday, 03 February 2010, 04:25 PM -0800):
> Not really sure if I should be posting this in the Zend Framework or Dojo
> mailing list, but here we go...
> 
> How to easily get post data in php/zf if it's json instead of 'normal' query
> string? 
> 
> i.e. an ajax http post is made and in firebug the following is listed under
> the post tab:
> 
> Source
> {"title":"asfdasdfasdfasdfasdf","seo_url":"asfdasdfasdfasdfasdf","author":"asfdasfd","sectionid":"7","summary":"awfawf","has_external_link":false,"external_link":"","body":"awfawfeawefawef","published":"on"}
> 
> screenshot:
> http://n4.nabble.com/file/n1462014/Capture.png 
> 
> This is the result of calling newItem({...data...}) followed by save() from
> dojox.data.JsonRestStore.
> 
> The data does not show up in the params of the zend framework request object
> or in $_POST.

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 Scrummer_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();
    }
}

-- 
Matthew Weier O'Phinney
Project Lead            | [email protected]
Zend Framework          | http://framework.zend.com/
PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc

Reply via email to