gpt4 book ai didi

php - 解析 PUT 请求的参数时出错

转载 作者:搜寻专家 更新时间:2023-10-31 21:33:47 26 4
gpt4 key购买 nike

当我尝试从 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;
}

}

并在主配置中包含这个类:

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

关于php - 解析 PUT 请求的参数时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24829383/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com