function get_attributes($products, $last_id='')
{
global $client_id, $api_key;
$product_id = [];
foreach ($products as $product) {
$product_id[] = (string)$product['product_id'];
}
$data = [
'filter' => [
'product_id' => $product_id,
'visibility' => 'ALL'
],
"limit" => 500,
"last_id" => $last_id
];
$headers = [
'Client-Id: ' . $client_id,
'Api-Key: ' . $api_key,
'Content-Type: application/json'
];
$curl = curl_init('https://api-seller.ozon.ru/v3/products/info/attributes');
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($curl, CURLOPT_TIMEOUT, 500);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$return = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
$res = 'cURL Err #' . $err;
} else {
$res = json_decode($return, true);
}
return $res;
}
if (!file_exists(__DIR__ . '/products_attributes.json') && file_exists(__DIR__ . '/products_id.json')) {
# products_id многомерный массив id товаров по 500 шт
$products_id = json_decode(file_get_contents(__DIR__ . '/products_id.json'), 1);
$products_atributes = [];
foreach ($products_id as $id) {
# Передано 500 id, но получил первые 100 товаров и last_id
$products_arr = get_attributes($id);
$products_atributes = array_merge($products_atributes, $products_arr['result']);
$last_id = $products_arr['last_id'];
while ($products_arr['result']) {
#Получил ошибку item not found
$products_arr = get_attributes($id, $last_id);
$products_atributes = array_merge($products_atributes, $products_arr['result']);
$last_id = $products_arr['last_id'];
}
}
file_put_contents(__DIR__ . '/products_attributes.json', json_encode($products_atributes, JSON_UNESCAPED_UNICODE));
}