Skip to content

Instantly share code, notes, and snippets.

@andronex
Created September 8, 2023 21:49
Show Gist options
  • Save andronex/54f86b3d7661b72e83667ae1a1d45b19 to your computer and use it in GitHub Desktop.
Save andronex/54f86b3d7661b72e83667ae1a1d45b19 to your computer and use it in GitHub Desktop.
Парсер цветов Ceresit (Церезит) палитр.
<?php
$tablePrefix = $modx->getOption(xPDO::OPT_TABLE_PREFIX);
$modx->addPackage('colorsproducts', $modx->getOption('core_path') . 'components/colorsproducts/model/', $tablePrefix);
$in = json_decode(file_get_contents('https://www.ceresit-coloursofnature.com/api/facade-designer/categories/6'), true);
print_r($in['data']['colours']);
foreach($in['data']['colours'] as $color){
if($color['images']['thumb']){
$name_img = mb_strtolower(end(explode('/', $color['images']['thumb'])));
$url_img = 'https://www.ceresit-coloursofnature.com/' . ltrim($color['images']['thumb'], '/');
if (!file_exists(MODX_ASSETS_PATH . 'images/colorceresit/' . $name_img)) {
$ch = curl_init($url_img);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
if(!file_put_contents(MODX_ASSETS_PATH . 'images/colorceresit/' . $name_img, curl_exec($ch))){
print_r("Неудача при скачивании картинки товара {$url_img}");
}
$code = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
if($code != 200){
print_r("Неудача при скачивании картинки товара {$url_img}, код {$code}");
}
curl_close($ch);
}
}
if($modx->getObject('ColorsProductsCeresit', ['name' => $color['name'], 'factura' => 'Мозаика'])) continue;
$newColor = $modx->newObject('ColorsProductsCeresit');
$newColor->set('name', $color['name']);
$newColor->set('hex', str_replace('#', '', $color['hex']));
$newColor->set('img', ltrim(MODX_ASSETS_URL . 'images/colorceresit/' . $name_img, '/'));
$newColor->set('factura', 'Мозаика');
$newColor->set('type', 'cokol');
$newColor->set('price', 500);
$newColor->set('price_kraski', 1200);
$newColor->set('price_gruntovki', 600);
$newColor->set('published', 1);
$newColor->save();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment