无法在PHP中获取'http put'内容

cra*_*ngs 2 php rest backbone.js

框架在这里 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);
Run Code Online (Sandbox Code Playgroud)

获取我的内容,但无法从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'];
}
Run Code Online (Sandbox Code Playgroud)

浏览器响应空白

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

Ben*_*Ben 6

正如我对您的问题发表评论的那样php://input,如果您从中读取它,它就会清空它.

我之前从未使用过Restler但是看了下载中提供的几个例子,它似乎表明提交的数据会自动作为参数传递给你的put处理程序.

在Restler的crud示例中,Author类有一个put请求,如下所示:

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

所以我猜这个restler已经读过了这个php://input流,因此把它清空了.

所以,你的put处理程序应该更像他们的例子:

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

编辑:实际上有一个来自@deceze的SO问题可以解释为什么从php://输入读取两次不起作用 - 对于PUT请求 - 这解释了为什么你的代码使用POST请求.无论哪种方式,你应该真正使用Restler提供的设施,而不是重新发明休息轮.