A
A
Anton2020-05-12 19:32:50
PHP
Anton, 2020-05-12 19:32:50

How to properly insert variable into json request body?

How to properly insert variable into json request body?
I do this and get an error

spoiler
{"error":{"code":"BAD_REQUEST","message":"Invalid request payload","data":[{"name":"old_price","code":"INVALID","value":" ","message":""},{"name":"price","code":"INVALID","value":" 7590 ","message":""},{"name":"premium_price","code":"INVALID","value":" 7135 ","message":""}]}} {"error":{"code":"BAD_REQUEST","message":"Invalid request payload","data":[{"name":"old_price","code":"INVALID","value":" 17490 ","message":""},{"name":"price","code":"INVALID","value":" 12243 ","message":""},{"name":"premium_price","code":"INVALID","value":" 11508 ","message":""}]}}

$data = '{
  "items": [{
    "description": " ' . $description . ' ",
    "category_id": " ' . $category_id . ' ",
    "name": " ' . $name . ' ",
    "offer_id": " ' . $offer_id . ' ",
    "price": " ' . $price . ' ",
    "old_price": " ' . $old_price . ' ",
    "premium_price": " ' . $premium_price . ' "
}]
}';

second part:
spoiler
$clientId = '2222222'; 
$apiKey = 'd2bfe812'; 
$method = '/v1/product/import'; 
$result = post($clientId, $apiKey, $method, $data); 
echo $result; 
function post($clientId, $apiKey, $method, $data){
  $url = 'http://api-seller.ozon.ru'.$method;
  $headers = array(                                                                          
    'Content-Type: application/json',
    'Host: api-seller.ozon.ru',
      'Client-Id: '.$clientId,
    'Api-Key: '.$apiKey
  ) ;                                                                                  
  $ch = curl_init();
  $options = array(
    CURLOPT_URL => $url,         
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POSTFIELDS => $data,
    CURLOPT_HTTPHEADER => $headers
  );    
  curl_setopt_array($ch, $options);
  $html = curl_exec($ch);
  curl_close($ch); 
  return $html;
};

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MagicMight, 2020-05-12
@anton99zel

Make an associative array and pack it via json_encode

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question