loadHTML($html);
// Search for the product link
$xpath = new DOMXPath($dom);
$productLinks = $xpath->query("//a[contains(@href, '/products/')]");
foreach ($productLinks as $productLink) {
$href = $productLink->getAttribute('href');
if (stripos($href, "/products/" . strtolower($code) . "-") !== false) {
return "https://colombia.cdopromocionales.com" . $href;
}
}
return null;
}
$products = fetchAllProducts($apiUrl);
// — Verificar que la API devuelva datos; reintentar 2 veces si viene vacío —
$retry = 0;
$maxRetry = 2;
while ($retry < $maxRetry && empty($products)) {
sleep(5); // esperar 5 s antes de reintentar
$products = fetchAllProducts($apiUrl);
$retry++;
}
if (empty($products)) {
die("El api está desactivado");
}
$mysqli = new mysqli("localhost", "ndconsulta", "Nitro2021", "db_inventarios_compranet");
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
// Truncate tables before inserting new data
$mysqli->query("TRUNCATE TABLE stocksur_iconos");
$mysqli->query("TRUNCATE TABLE stocksur_productos");
$mysqli->query("TRUNCATE TABLE stocksur_variantes");
foreach ($products as $product) {
// Check if the indices exist before accessing them
$product['name'] = isset($product['name']) ? str_replace(["'", '"', '”', '¨', '“', '“', '\\'], '', $product['name']) : '';
$product['description'] = isset($product['description']) ? str_replace(["'", '"', '”', '“', '“'], '', $product['description']) : '';
$product['code'] = isset($product['code']) ? $product['code'] : '';
$width = isset($product['packing']['width']) ? $product['packing']['width'] : null;
$height = isset($product['packing']['height']) ? $product['packing']['height'] : null;
$depth = isset($product['packing']['depth']) ? $product['packing']['depth'] : null;
$volume = isset($product['packing']['volume']) ? $product['packing']['volume'] : null;
$quantity = isset($product['packing']['quantity']) ? $product['packing']['quantity'] : null;
$weight = isset($product['packing']['weight']) ? $product['packing']['weight'] : null;
$urlExterna = getProductUrl($product['code']);
$product['name'] = ucfirst(strtolower($product['name']));
$stmt = $mysqli->prepare("INSERT INTO stocksur_productos (id_producto, code, name, description, width, height, depth, volume, quantity, weight, urlExterna) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON DUPLICATE KEY UPDATE code=VALUES(code), name=VALUES(name), description=VALUES(description), width=VALUES(width), height=VALUES(height), depth=VALUES(depth), volume=VALUES(volume), quantity=VALUES(quantity), weight=VALUES(weight), urlExterna=VALUES(urlExterna)");
$stmt->bind_param("isssssssiss", $product['id'], $product['code'], $product['name'], $product['description'], $width, $height, $depth, $volume, $quantity, $weight, $urlExterna);
$stmt->execute();
foreach ($product['variants'] as $variant) {
$discount = ($variant['list_price'] == $variant['net_price']) ? 0 : 1;
$stmt = $mysqli->prepare("INSERT INTO stocksur_variantes (id_variante, id_producto, code, color, stock_available, stock_existent, list_price, net_price, discount, picture_small, picture_medium, picture_original, detail_picture_small, detail_picture_medium, detail_picture_original) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON DUPLICATE KEY UPDATE id_producto=VALUES(id_producto), code=VALUES(code), color=VALUES(color), stock_available=VALUES(stock_available), stock_existent=VALUES(stock_existent), list_price=VALUES(list_price), net_price=VALUES(net_price), discount=VALUES(discount), picture_small=VALUES(picture_small), picture_medium=VALUES(picture_medium), picture_original=VALUES(picture_original), detail_picture_small=VALUES(detail_picture_small), detail_picture_medium=VALUES(detail_picture_medium), detail_picture_original=VALUES(detail_picture_original)");
$stmt->bind_param("iissiiddissssss", $variant['id'], $product['id'], $product['code'], $variant['color']['name'], $variant['stock_available'], $variant['stock_existent'], $variant['list_price'], $variant['net_price'], $discount, $variant['picture']['small'], $variant['picture']['medium'], $variant['picture']['original'], $variant['detail_picture']['small'], $variant['detail_picture']['medium'], $variant['detail_picture']['original']);
$stmt->execute();
}
foreach ($product['icons'] as $icon) {
$stmt = $mysqli->prepare("INSERT INTO stocksur_iconos (id_producto, label) VALUES (?, ?)
ON DUPLICATE KEY UPDATE label=VALUES(label)");
$stmt->bind_param("is", $product['id'], $icon['label']);
$stmt->execute();
}
}
// Call the stored procedure actualizar_pvp
$call_query = "CALL actualizar_stocksur;";
mysqli_query($mysqli, $call_query);
// Check for errors in calling the stored procedure
if (mysqli_error($mysqli)) {
echo "Error al llamar al procedimiento almacenado: " . mysqli_error($mysqli) . "
";
} else {
echo "Procedimiento almacenado ejecutado correctamente
";
}
$mysqli->close();
$HoraFin = date('h:i:s') . "\n";
echo $HoraFin;
echo "Proceso terminado.";
?>
Proceso terminado. Mostrando barra de progreso...