gpt4 book ai didi

php - PHP中无法获取 'http put'内容

转载 作者:行者123 更新时间:2023-12-02 14:30:51 28 4
gpt4 key购买 nike

框架在这里 http://luracast.com/products/restler/

我在工作中使用reSTLer作为restful api,当我使用主干模型保存到 URL 时,它会使用“HTTP PUT”请求方法以 json 形式发送和更新我的数据,并且我想从我输入的内容中获取响应...

如果它是我可以使用的 HTTP POST 请求方法

// to getting content from a POST
$post_data = json_decode(file_get_contents('php://input'), true);

获取我的内容,但无法从 HTTP PUT 获取任何内容

// can't get anything from a PUT
function putpatients($id) {
$post_data = file_get_contents('php://input');
$post_data = json_decode($post_data, true);
echo $post_data['name'];
}

浏览器响应空白

如何以 json 形式返回数据???

最佳答案

正如我对你的问题的评论,php://input 是一个流,如果你从中读取,它会清空它。

我以前从未使用过 ReSTLer,但查看其下载中提供的几个示例,似乎表明提交的数据会自 Action 为参数传递给您的 put 处理程序。

在雷斯特勒的 crud example ,Author 类有一个这样的 put 请求:

function put($id=NULL, $request_data=NULL) {
return $this->dp->update($id, $this->_validate($request_data));
}

所以我猜测 ReSTLer 已经读取了 php://input 流,因此清空了它。

所以,您的放置处理程序可能应该更像他们的示例:

function putpatients($id, $request_data = NULL) {
/* do something with the $request_data */
var_dump($request_data);
}

Edit: There's actually a previous SO question from @deceze that talks about why reading twice from php://input doesn't work - for PUT requests - which explains why your code worked with a POST request. Either way, you should really use the facility provided by Restler rather than re-inventing the rest wheel.

关于php - PHP中无法获取 'http put'内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9513355/

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