Всем привет!
Пытаюсь добавить товары на Ozon через API, пользуюсь PHP guzzle http.
Вот код моего класса для работы с API Ozon:
namespace App\Models\Services;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
class ApiOzon
{
protected $client;
/**
* ApiOzon constructor.
*/
public function __construct()
{
$this->client = new Client([
'headers' => [
'Client-Id' => config('app.ozon.client-id'),
'Api-Key' => config('app.ozon.api-key'),
'Content-Type' => 'application/json'
],
'base_uri' => config('app.ozon.base_uri')
]);
}
/**
* Add products
*
* @param array $items
* @return array|mixed
*/
public function addProducts(array $items)
{
try {
$response = $this->client
->post('/v2/product/import', $items)->getBody()->getContents();
} catch (GuzzleException $e) {
return ['error' => $e->getMessage()];
}
return json_decode($response, true);
}
}
Вызываю так:
public function testAddProducts()
{
$items = [
'items' => [
[
'attributes' => [
[
'id' => 0,
]
],
'category_id' => 0,
'depth' => 200,
'dimension_unit' => 'mm',
'height' => 200,
'images' => ['string'],
'offer_id' => 0,
'price' => '100',
'vat' => 0,
'weight' => 1,
'weight_unit' => 'kg',
'width' => 200,
],
],
];
$client = new ApiOzon();
$response = $client->addProducts($items);
dd($response);
}
Получаю ошибку:
array:1 [
"error" => """
Client error: `POST http://cb-api.ozonru.me/v2/product/import` resulted in a `400 Bad Request` response:\n
{"error":{"code":"BAD_REQUEST","message":"Invalid JSON payload","data":[{"name":"cause","code":"","value":"EOF","message (truncated...)\n
"""
]
Что я делаю не так?
Вот документация:
Добавить товары