За последние 24 часа нас посетили 20642 программиста и 1103 робота. Сейчас ищут 386 программистов ...

Как правильно получить атрибуты товаров Озон?

Тема в разделе "PHP для профи", создана пользователем zahar_92, 20 июл 2022.

Метки:
  1. zahar_92

    zahar_92 Активный пользователь

    С нами с:
    8 окт 2016
    Сообщения:
    10
    Симпатии:
    0
    Всем привет!
    Пытаюсь получить атрибуты своих товаров на озон, но получаю только первые 100, при этом значение limit не играет роли, хотя в доках указано, что можно использовать limit от 1 до 1000.
    Всего 5200 товаров, я передаю первые 500 id товаров и limit=500, но все равно получаю только первые 100 из переданных 500.
    Насколько я понял, нужно использовать last_id после первого запроса, но цикл возвращает пустой массив и сообщение, что товары кончились. Прошу помочь и объяснить, как правильно получить все 5200 товаров.

    Код (Text):
    1.  
    2. function get_attributes($products, $last_id='')
    3. {
    4.     global $client_id, $api_key;
    5.  
    6.     $product_id = [];
    7.     foreach ($products as $product) {
    8.         $product_id[] = (string)$product['product_id'];
    9.     }
    10.  
    11.     $data = [
    12.         'filter' => [
    13.             'product_id' => $product_id,
    14.             'visibility' => 'ALL'
    15.         ],
    16.         "limit" => 500,
    17.         "last_id" => $last_id
    18.     ];
    19.  
    20.     $headers = [
    21.         'Client-Id: ' . $client_id,
    22.         'Api-Key: ' . $api_key,
    23.         'Content-Type: application/json'
    24.     ];
    25.  
    26.     $curl = curl_init('https://api-seller.ozon.ru/v3/products/info/attributes');
    27.     curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    28.     curl_setopt($curl, CURLOPT_HEADER, false);
    29.     curl_setopt($curl, CURLOPT_POST, true);
    30.     curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
    31.     curl_setopt($curl, CURLOPT_TIMEOUT, 500);
    32.     curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    33.    
    34.     $return = curl_exec($curl);
    35.     $err = curl_error($curl);
    36.     curl_close($curl);
    37.  
    38.     if ($err) {
    39.         $res = 'cURL Err #' . $err;
    40.     } else {
    41.         $res = json_decode($return, true);
    42.     }
    43.  
    44.     return $res;
    45. }
    46.  
    47. if (!file_exists(__DIR__ . '/products_attributes.json') && file_exists(__DIR__ . '/products_id.json')) {
    48.     # products_id многомерный массив id товаров по 500 шт
    49.     $products_id = json_decode(file_get_contents(__DIR__ . '/products_id.json'), 1);
    50.     $products_atributes = [];
    51.  
    52.     foreach ($products_id as $id) {
    53.         # Передано 500 id, но получил первые 100 товаров и last_id
    54.         $products_arr = get_attributes($id);
    55.         $products_atributes = array_merge($products_atributes, $products_arr['result']);
    56.         $last_id = $products_arr['last_id'];
    57.  
    58.         while ($products_arr['result']) {
    59.             #Получил ошибку item not found
    60.             $products_arr = get_attributes($id, $last_id);
    61.             $products_atributes = array_merge($products_atributes, $products_arr['result']);
    62.             $last_id = $products_arr['last_id'];
    63.         }
    64.     }
    65.  
    66.     file_put_contents(__DIR__ . '/products_attributes.json', json_encode($products_atributes, JSON_UNESCAPED_UNICODE));
    67. }

    Используемый метод OZON API
     
  2. ADSoft

    ADSoft Старожил

    С нами с:
    12 мар 2007
    Сообщения:
    3.816
    Симпатии:
    735
    Адрес:
    Татарстан
    а вы не путаете получение товаров и атрибутов товаров?
    может атрибутов реально 100? или менее?