G
G
Gleb Starkov2020-10-23 13:23:31
PHP
Gleb Starkov, 2020-10-23 13:23:31

How to add products to ozon via its API?

Hello!
I'm trying to add products to Ozon via API using PHP guzzle http.

Here is my class code for working with the Ozon API:

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);
    }
}


I call like this:
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);
    }


I am getting an error:
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
    """
]


What am I doing wrong?

Here is the documentation:
Add Products

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Antony, 2020-10-23
@colonel

Invalid JSON payload

First, look at what you are actually sending and compare it with the documentation.

M
MKE, 2020-10-25
@MKE

We are also going to start working with Ozon.
We decided to use a ready-made solution: we already have XML2Ozon XML in the YML format. Perhaps useful.

A
Alexey Gnevyshev, 2021-11-23
@iResource

Сервис Tovaroved.net берёт интеграцию на себя. От продавца нужен только YML-фид с данными.
Для новых продавцов - бесплатно!
Это возможно за счет статуса сертифицированного технологического партнёра Ozon.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question