paragi / sphp

A snappy PHP execution module / middleware
46 stars 14 forks source link

php://input empty #16

Closed ghost closed 4 years ago

ghost commented 4 years ago

Hi,

I'm posting JSON data to my PHP backend. But I'm unable to get the data because php://input remains empty. Tried the same post on a appache server and everything seams to work fine. Am i missing a php lib? Or is this a know bug.

ghost commented 4 years ago

Tried an other node php library and the issue isn't there.

paragi commented 4 years ago

Thank you for reporting a problem. Please provide a working example.

ghost commented 4 years ago

I'm putting some form data in a json object and sending it to the server.

return fetch( process.env.VUE_APP_BACKEND_BASEURL + "/RestController.php?page_key=create", { method: "POST", body: JSON.stringify(wish), headers: { Accept: "application/json" } } ).then(response => response.json()); Then in the backend. $_POST = json_decode(file_get_contents('php://input'), true);

paragi commented 4 years ago

OK I think I see the problem. The structure of SPHP is that the worker is spawned preemptively. In order to do that php://input is used to stall, transfer server environment and the request, before your script is executed. Therefore the stream is empty, when your script runs.

In short: I'm sorry. to say, that you can not use php://input that way with SPHP.

What you could do with SPHP is to store the JSON in a POST.

ghost commented 4 years ago

Oke well thanks for the time. I fixed it by running an Apache server and a node reverse proxy.