What's the difference between posting from a f

2019-09-03 09:39发布

问题:

On a php-server I capture all posted variables from $_POST, forexample

var_dump($_POST);

This works fine if I use a html-form to post to the server.

I try to post to the server using nodejs and requestify:

requestify.post('http://localhost/rest/1/comment/create', {hello: 'world'})
.then(function(response) {
    console.log(response.getBody());
})

This will not be captured by the php-server and var_dump($_POST) outputs an empty array.

回答1:

When you submit an HTML form, the data will be encoded using the application/x-www-form-urlencoded format (unless you tell it to use text/plain or multipart/form-data).

PHP will automatically parse application/x-www-form-urlencoded and multipart/form-data into $_POST.

Restify will encode data as application/json, which PHP won't parse by default. This question details how to read JSON formatted POST requests.



回答2:

You can use json_decode(file_get_contents("php://input"), true); snippet to read json request from PHP. php://input is PHP's STDIN where raw request is available. Details also available in docs.



标签: php node.js http