简体   繁体   English

解析PUT请求的参数时出错

[英]Error when parsing parameters for a PUT request

I'm having a problem when I trying to get parameters from a PUT request on Yii. 当我尝试从Yii上的PUT请求获取参数时,我遇到了问题。

The method getPut() calls the method getRestParams(), that method tries to parse the rawBody. 方法getPut()调用方法getRestParams(),该方法尝试解析rawBody。 The method getRawBody() returns the value of file_get_contents('php://input') 方法getRawBody()返回file_get_contents的值('php:// input')

The function file_get_contents('php://input') returns the value: 函数file_get_contents('php:// input')返回值:

{"foo": "foo content", "bar": "bar content"}

The function getRestParams() tries to parse this content calling the function mb_parse_str ( https://github.com/yiisoft/yii/blob/master/framework/web/CHttpRequest.php#L290 ) 函数getRestParams()尝试解析此内容,调用函数mb_parse_str( https://github.com/yiisoft/yii/blob/master/framework/web/CHttpRequest.php#L290

mb_parse_str($this->getRawBody(), $result);

But this returns: 但这回归:

Array
(
    [{"foo": "foo content", "bar": "bar content"}] => 
)

I haven't much experience with PHP, much less with the Yii framework. 我对PHP没有太多经验,更不用说Yii框架了。

To me, it seems to be a bug in the framework, but it is likely that I'm wrong. 对我而言,它似乎是框架中的一个错误,但很可能是我错了。

Could anyone help me with this? 任何人都可以帮我这个吗?

If you expect all the requests to have JSON data use this simple solution: 如果您希望所有具有JSON数据的请求都使用这个简单的解决方案:

Extend base request component: 扩展基本请求组件:

class HttpJsonRequest extends CHttpRequest
{

    protected $_restParams;

    public function getRestParams()
    {
        if ($this->_restParams === null) {
            $this->_restParams = CJSON::decode($this->getRawBody());
        }

        return $this->_restParams;
    }

}

And include this class in config main: 并在config main中包含此类:

'components' => array(
    ...
    'request' => array('class' => 'HttpJsonRequest'),
    ...
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM