尝试从Yii上的PUT请求获取参数时遇到问题。
方法getPut()调用方法getRestParams(),该方法尝试解析rawBody。方法getRawBody()返回file_get_contents('php:// input')的值
函数file_get_contents('php:// input')返回值:
{"foo": "foo content", "bar": "bar content"}
函数getRestParams()尝试解析此内容,调用函数mb_parse_str(https://github.com/yiisoft/yii/blob/master/framework/web/CHttpRequest.php#L290)
mb_parse_str($this->getRawBody(), $result);
但这返回:
Array
(
[{"foo": "foo content", "bar": "bar content"}] =>
)
我对PHP没有太多的经验,而对Yii框架的了解则更少。
对我来说,这似乎是框架中的错误,但我可能错了。
有人可以帮我吗?
如果您希望所有请求都具有JSON数据,请使用以下简单解决方案:
扩展基本请求组件:
class HttpJsonRequest extends CHttpRequest
{
protected $_restParams;
public function getRestParams()
{
if ($this->_restParams === null) {
$this->_restParams = CJSON::decode($this->getRawBody());
}
return $this->_restParams;
}
}
并在config main中包含此类:
'components' => array(
...
'request' => array('class' => 'HttpJsonRequest'),
...
)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句