I am very new to the world of PHH and I am trying to implement some Rest API.
I have installed a MAMP server on my MAC. Then I wrote the following php code in api.php:
<?php
$api = new RestUtils;
$api->processRequest();
class RestUtils
{
public static function processRequest()
{
$request_method = strtolower($_SERVER['REQUEST_METHOD']);
var_dump($request_method);
var_dump($_POST);
var_dump($_REQUESTS);
}
}
?>
Then in my terminal window, I execute the following:
curl -X POST -H "Content-Type: application/json" -d '{"name": "john"}' http://localhost:8888/api.php
I get the following output:
string(4) "post"
array(0){ }
array(0){ }
When I call call
echo file_get_contents("php://input");
it returns {"name":"john"}
You're posting a raw string. If you're expecting to PHP to process that string into $_POST, you have to submit it as a key=value
pair, e.g.
-d 'foo:{"name":"john"}'
and then
echo $_POST['foo'];