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
Edit as you wish
function getPostObject() {
$str = file_get_contents('php://input');
$std = json_decode($str);
if ($std === null) {
$std = new stdClass();
$array = explode('&', $str);
foreach ($array as $parm) {
$parts = explode('=', $parm);
if(sizeof($parts) != 2){
continue;
}
$key = $parts[0];
$value = $parts[1];
if ($key === NULL) {
continue;
}
if (is_string($key)) {
$key = urldecode($key);
} else {
continue;
}
if (is_bool($value)) {
$value = boolval($value);
} else if (is_numeric($value)) {
$value += 0;
} else if (is_string($value)) {
if (empty($value)) {
$value = null;
} else {
$lower = strtolower($value);
if ($lower === 'true') {
$value = true;
} else if ($lower === 'false') {
$value = false;
} else if ($lower === 'null') {
$value = null;
} else {
$value = urldecode($value);
}
}
} else if (is_array($value)) {
// value is an array
} else if (is_object($value)) {
// value is an object
}
$std->$key = $value;
}
// length of post array
//$std->length = sizeof($array);
}
return $std;
}
I had got this error too, my solution is to change the data format to raw.
I get it from php doc where it says
php://input
is not available with enctype="multipart/form-data".
I see the problem,
You need to add filename.php in the end of the request url or need to rewrite the server rules in .htaccess file to get around this.
curl -i -X PUT -d '{"address":"Sunset Boulevard"}' http://localhost/clients/ryan/{filename.php}
replace {filename.php} with appropriate file name. :)
Make sure there are no redirects.
file_get_contents(php://input)
doesn't pass the body content through redirects.
One quick way to check for redirects is to check the url with curl. On the command line:
curl -IL http://example.com/api
{
"action":"get_events_by_category",
"category_id":2
}
Your row string data must be a Double quote "" not use single ''
On Windows the combination of 'single "double" quotes' does not seem to work. Use escape for quotes in your json data (as below) & it should work
curl -X PUT -d "{\"address\":\"Sunset Boulevard\"}" http://localhost/clients/ryan