I\'m building a PHP RESTful API, following this tutorial. The following function, which should return the data sent with the request when the \'put\' method is used, returns
As noted elsewhere on the web, php://input
content does not get through if request is redirected. Maybe your web server has a redirect from an URL starting with www to an URL without www, or from URLs using HTTP to URLs using HTTPS. Check your web server logs to verify this, and act accordingly to avoid the redirection when making calls to the web service.
I had the same problem. Eventually, I found that the problem was that in the client side I didn't stringify the json object (previously I used nodejs server and it was OK). Once I did that, I received the data in $_POST (not as json), as regular parameters.
I wrote a headerfile with this code:
if($_SERVER["REQUEST_METHOD"] == "POST" && $_SERVER["CONTENT_TYPE"] == "application/json")
{
$data = file_get_contents("php://input", false, stream_context_get_default(), 0, $_SERVER["CONTENT_LENGTH"]);
global $_POST_JSON;
$_POST_JSON = json_decode($_REQUEST["JSON_RAW"],true);
// merge JSON-Content to $_REQUEST
if(is_array($_POST_JSON)) $_REQUEST = $_POST_JSON+$_REQUEST;
}
It checks for the correct content-type and it reads only as much post input, like specified in Content-Length header. When receiving a valid JSON it created an global Array $_POST_JSON.
So you can work with your JSON-Content the similiar like you do it with url-encoded POST values.
Example:
echo $_POST_JSON["address"];
// or
echo $_REQUEST["address"];
for me this problem started, suddenly.
i had installed ssl certificate.
when i checked the response code it showed me 301, then i realized and changed
http://
to
https://
and got all the request back with file_get_contents('php://input').
in your example curl call:
put s after http as shown below:
curl -i -X PUT -d '{"address":"Sunset Boulevard"}' https://localhost/clients/ryan
First off, i was able to run this code and it worked fine:
--Terminal---------
//I ran this curl request against my own php file:
curl -i -X PUT -d '{"address":"Sunset Boulevard"}' http://localhost/test.php
--PHP--------------
//get the data
$json = file_get_contents("php://input");
//convert the string of data to an array
$data = json_decode($json, true);
//output the array in the response of the curl request
print_r($data);
If that doesn't work, check the console for errors and your php settings:
tail -f /path/to/the/php/log/file
so you can actually see the output of these php calls.file_get_contents(file://input): failed to open stream: no suitable wrapper could be found
which can indicate either a typo of the "file://input" string or the fact that allow_url_fopen
is disabled in php (see #5 if unsure)remember, file_get_contents
only works when allow_url_fopen
is set to true in your PHP settings. thats something that is set in php.ini, but you can also change settings at run time by writing something along the lines of the following code before the other code:
ini_set("allow_url_fopen", true);
The correct function call is:
file_get_contents("php://input");
You should be getting an error message saying that php:input
doesn't exist...
In any case, PUT
is intended for uploading files to the server, assuming the server supports it. Usually you'd use POST
with a Content-Type
header appropriate to the content.