O
O
ovignatov2019-07-18 12:52:53
PHP
ovignatov, 2019-07-18 12:52:53

How to accept POST json request in php?

Hello! There was a question with which I am excruciated already some days.
There is a service that sends a request in json format to the specified POST page, but I need to catch it on my page and add elements to the database.
The documentation says:
Request example:

POST /page.php HTTP/1.1
Host: host.host
Authorization: Bearer XXXXX
Content-Type: application/json
Accept: application/json
Cache-Control: no-cache
{
"date": "2017-04-25T01:29:20.4520214+00:00",
"stocks": [
    {
    "nnt": 111173,
    "qnt": 5,
    "man": "man",
    "prcRet": 117.99,
    "name": "name",
    "dtComing": "2017-08-09T00:00:00 + 07:00"
    }
 ]
}

Example of a successful response:
HTTP/1.1 202 Accepted
[]

I'm trying to catch via php
header('content-type: application/json');
$v = json_decode(file_get_contents('php://input'));
echo json_encode($v);

But on the page I get NULL, although the service says that everything is good. Can you please tell me how to get this json?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry Arushanov, 2019-07-18
@daruwanov

$_POST?

P
profesor08, 2019-07-18
@profesor08

parsiphp://input

// твои данные тут
$input = json_decode(file_get_contents("php://input"), true);

If it is empty, then go further along the chain: https://php.net/manual/en/wrappers.php.php
Go through and find everything that flies to you.

I
irishmann, 2019-07-18
@irishmann

See here for a similar situation.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question