A
A
andrey_levushkin2020-07-21 12:45:08
PHP
andrey_levushkin, 2020-07-21 12:45:08

How to send JSON in the body of a POST request?

I need to put json like this:

{
  "records": [
    {
      "fields": {
        "Name": "111",
        "Phone": "222",
        "Status": "333"
      }
    }
  ]
}

In the body of the POST request, which is sent like this:
<?
$url = "адрес_запроса"; // URL-адрес POST 
$options = array(
  'http' => array(
    'method'  => 'POST',
    'content' => json_encode( $data ),
    'header'=>  "Content-Type: application/json\r\n" .
                "Authorization: Bearer XXX\r\n"
    )
);

$context  = stream_context_create( $options );
$result = file_get_contents( $url, false, $context );
$response = json_decode( $result );
?>

As I understand it, you need to put it in the $data array. But how to keep exactly this structure?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2020-07-21
@andrey_levushkin

$data = [
  "records" => [
    [
      "fields" => [
        "Name" => "111",
        "Phone" => "222",
        "Status" => "333"
      ]
    ]
  ]
];

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question