php处理来自backbone.js的请求

big*_*ind 4 php put backbone.js

当backbone.js将模型保存到服务器时,它会发送PUT请求.我如何用PHP处理这些?如何获取随put请求发送的内容,并将它们存储在数据库中?

pap*_*982 8

这是另一个例子:

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

  • 这将导致一个Array(json_decode()的第二个参数)$ values,它将包含你收到的json数据的key => value对.


Ruf*_*nus 5

请参阅php文档以获取示例http://php.net/manual/en/features.file-upload.put-method.php

来自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);
?>
Run Code Online (Sandbox Code Playgroud)

当您想要将数据存储到数据库时,可以将fwrite部分保留.