简体   繁体   English

php 处理来自 backbone.js 的放置请求

[英]php handle put request from backbone.js

When backbone.js saves a model to the server, it sends a PUT request.当 backbone.js 将 model 保存到服务器时,它会发送 PUT 请求。 How do I handle these with php?如何使用 php 处理这些问题? How do I take the contents that are sent with the put request, and store them in a database?如何获取与 put 请求一起发送的内容,并将它们存储在数据库中?

Here is another example:这是另一个例子:

$values = json_decode(file_get_contents('php://input'), true); $values = json_decode(file_get_contents('php://input'), true);

  • This would result in an Array (second parameter of json_decode()) $values which would contain your key => value pairs of the received json data.这将产生一个数组(json_decode() 的第二个参数)$values,其中将包含接收到的 json 数据的键 => 值对。

see the php docs for an example http://php.net/manual/en/features.file-upload.put-method.php有关示例,请参阅 php 文档http://php.net/manual/en/features.file-upload.put-method.php

from php.net:来自 php.net:

<?php
/* PUT data comes in on the stdin stream */
$putdata = fopen("php://input", "r");

/* Open a file for writing */
$fp = fopen("myputfile.ext", "w");

/* Read the data 1 KB at a time
   and write to the file */
while ($data = fread($putdata, 1024))
  fwrite($fp, $data);

/* Close the streams */
fclose($fp);
fclose($putdata);
?>

you can leave the fwrite part out when you want to store the data to a DB.当您想将数据存储到数据库时,您可以将 fwrite 部分排除在外。

Backbone.emulateHTTP = true;
If you want to work with a legacy web server that doesn't support Backbones's default REST/HTTP approach, you may choose to turn on Backbone.emulateHTTP. 如果您想使用不支持 Backbones 默认 REST/HTTP 方法的旧版 web 服务器,您可以选择打开 Backbone.emulateHTTP。 Setting this option will fake PUT and DELETE requests with a HTTP POST, and pass them under the _method parameter. 设置此选项将使用 HTTP POST 伪造 PUT 和 DELETE 请求,并在 _method 参数下传递它们。 Setting this option will also set an X-HTTP-Method-Override header with the true method. 设置此选项还将使用 true 方法设置 X-HTTP-Method-Override header。

After that implement your own sync function in your model: http://documentcloud.github.com/backbone/#Sync之后在您的 model 中实现您自己的sync function: http://documentcloud.ZBF215181B514057#SyncFAZ.com/backbone/B7354417

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

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