Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php handle put request from backbone.js

When backbone.js saves a model to the server, it sends a PUT request. How do I handle these with php? How do I take the contents that are sent with the put request, and store them in a database?

like image 504
bigblind Avatar asked Dec 04 '22 08:12

bigblind


2 Answers

Here is another example:

$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.
like image 72
honederr82 Avatar answered Dec 25 '22 18:12

honederr82


see the php docs for an example http://php.net/manual/en/features.file-upload.put-method.php

from 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.

like image 28
Rufinus Avatar answered Dec 25 '22 19:12

Rufinus