Working with API OZONE
The last notes
All English-language materials have been translated fully automatically using the Google service
Get the rest of all products in the store on Ozone via API
function getProductBalance($page = 1){
$url = 'http://api-seller.ozon.ru/v1/product/info/stocks';
$headers = [
'Content-Type: application/json',
'Host: api-seller.ozon.ru',
'Client-Id: ' . $CLIENT_ID,
'Api-Key: ' . $API_CODE
];
$data = [
'page' => $page,
'page_size' => '1000',
];
$options = [
CURLOPT_URL => $url,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => json_encode($data),
CURLOPT_HTTPHEADER => $headers
];
$ch = curl_init();
curl_setopt_array($ch, $options);
$result = curl_exec($ch);
curl_close($ch);
//std to array
return json_decode(json_encode(json_decode($result)), True);
}
$result = getProductBalance();
// Calculate the number of goods received on the first request
$number = count($result['result']['items']);
// Get the total number of products
$total = $result['result']['total'];
// Calculate the number of requests required to get the total number with the current step
$pages = ceil($total / $number);
// Initiate a temporary variable where we will add newly received products
$items = $result['result']['items'];
for($i = 2; $i < $pages + 1; ++$i) {
$result = getProductBalance($i);
$items = array_merge($items,$result['result']['items']);
}
$result = [];
foreach ($items as $k => $v){
$result[$v['offer_id']] = $v;
}
Get information from Ozone about one product by its id
function getStockById($data){
$url = 'http://api-seller.ozon.ru/v2/product/info';
$headers = [
'Content-Type: application/json',
'Host: api-seller.ozon.ru',
'Client-Id: ' . $CLIENT_ID,
'Api-Key: ' . $API_CODE
];
$options = [
CURLOPT_URL => $url,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => json_encode($data),
CURLOPT_HTTPHEADER => $headers
];
$ch = curl_init();
curl_setopt_array($ch, $options);
$result = curl_exec($ch);
curl_close($ch);
//std to array
return json_decode(json_encode(json_decode($result)), True);
}
// Usage
$IDS = [
'offer_id' => '52523',
];
$product = getStockById($IDS);
We receive information about specific products (together with leftovers) by their id
function getStocksById($data){
$url = 'http://api-seller.ozon.ru/v2/product/info/list';
$headers = [
'Content-Type: application/json',
'Host: api-seller.ozon.ru',
'Client-Id: ' . $CLIENT_ID,
'Api-Key: ' . $API_CODE
];
$options = [
CURLOPT_URL => $url,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => json_encode($data),
CURLOPT_HTTPHEADER => $headers
];
$ch = curl_init();
curl_setopt_array($ch, $options);
$result = curl_exec($ch);
curl_close($ch);
//std to array
return json_decode(json_encode(json_decode($result)), True);
}
// Использование
$IDS = [
'offer_id' => ['77777', '55555'],
'product_id' => [],
'sku' => [],
];
$products = getStocksById($IDS);
Comments