D
D
doorway-team2018-09-13 16:07:16
PHP
doorway-team, 2018-09-13 16:07:16

Why isn't JSON sent to the API via a POST request?

I'm trying to send data as a JSON POST request to the RetailCRM service API. However, for some reason, the interaction does not occur.
There is a code:

<?php
$url = 'https://domain.retailcrm.ru/api/v5/orders/create?apiKey=t2SlFkY8o7tLpsGpbzeluCHd5TbFkxfD';
$json = '
  {
    "site":"ID",
    "order":{
      "firstName":"Тест 001",
      "phone":"00000000000",
      "email":"[email protected]",
      "delivery":{
        "address":"Тестовый адрес..."
      },
      "customerComment":"Комментарий клиента...",
      "number":"Номер_Заказа",
      "items":[
        {
          "offer":{
            "externalId":"6005"
          },
          "quantity":"1"
        },
        {
          "offer":{
            "externalId":"6017"
          },
          "quantity":"5"
        }
      ]
    }
  }
';
$data = array(
    'site' => 'ID',
    'order' => array(
        'firstName' => 'Тест 001',
        'phone' => '00000000000',
        'email' => '[email protected]',
        'delivery' => array(
            'address' => 'Тестовый адрес...'
        ),
        'customerComment' => 'Комментарий клиента...',
        'number' => 'Номер_Заказа',
        'items' => [
        	array(
            	'offer' => array(
            		'externalId' => '6005'
        		),
        		'quantity' => '1'
        	),
        	array(
            	'offer' => array(
            		'externalId' => '6017'
        		),
        		'quantity' => '5'
        	)
        ]
    )
);
$postdata = http_build_query(json_encode($data));
$opts = array('http' =>
  array(
    'method'  => 'POST',
    'header'  => 'Content-type: application/json; charset=UTF-8',
    'content' => $postdata
  )
);
$context = stream_context_create($opts);
return file_get_contents($url, false, $context);
?>

I pass to the request both $json itself (json in its pure form), and $data through json_encode (I format the array in json)
What could be the problem? Maybe someone can help for $?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lander, 2018-09-13
@usdglander

$postdata = http_build_query(json_encode($data));
The line looks like shit.
You either put on panties, or take off the cross. Most likely your option:
although it is possible and
$postdata = http_build_query($data);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question