|
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: ![]() 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. Thanks! adam |
|
As usual my message wasn't excepted on my first try, but I think it always is when I reply to it myself...
|
|
In reply to this post by OakBehringer
Hi Adam,
> How to easily get post data in php/zf if it's json instead of 'normal' query > string? There are two possible solutions. Either you let the server transform the json data using Zend_Json, reading it from the raw post body using the http request's getRawBody() method. I am not sure if there is a neater way to inject the JSON values into the request object (probably there is, someone else might elaborate on that). Second solution is make sure the content is posted using a regular POST with a application/x-www-form-urlencoded content type (the same way a regular POST form is sent). That should be fixed in your Dojo (or other client side) code. It is a matter of taste what your preference is; however I always prefer handling forms with regular posts in ajax the same way I do without (which can save a lot of trouble getting your application non-js compatible, or have a working fallback if the JS somehow fails). HTH, Gerard |
|
Administrator
|
In reply to this post by OakBehringer
-- OakBehringer <[hidden email]> 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 | [hidden email] Zend Framework | http://framework.zend.com/ PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc |
|
In reply to this post by OakBehringer
Have you looked at opening/reading the php://input stream?
Paddy http://blog.astrumfutura.com http://www.survivethedeepend.com OpenID Europe Foundation Irish Representative From: OakBehringer <[hidden email]> To: [hidden email] Sent: Thu, February 4, 2010 1:50:50 AM Subject: [fw-general] Re: Getting JSON POST data from Zend? As usual my message wasn't excepted on my first try, but I think it always is when I reply to it myself... OakBehringer wrote: > > 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: > > 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. > > Thanks! > adam > -- View this message in context: http://n4.nabble.com/Getting-JSON-POST-data-from-Zend-tp1462014p1462075.html Sent from the Zend Framework mailing list archive at Nabble.com. |
|
In reply to this post by weierophinney
Thanks, that worked great. I had already written a similar plugin for the PUT verb, but I couldn't find the function getRawBody() for some reason.
Thanks! Adam |
| Powered by Nabble | Edit this page |
