"https://lavasemkhangi.ir/mixin-products.json", "cache_file" => __DIR__."/cache.xml", "log_file" => __DIR__."/log.txt", "cache_lifetime" => 600, // seconds ]; // ======================================== // LOGGER // ======================================== function logMessage($msg) { global $config; file_put_contents( $config["log_file"], date("Y-m-d H:i:s") . " | " . $msg . "\n", FILE_APPEND ); } // ======================================== // CACHE CHECK // ======================================== if (file_exists($config["cache_file"])) { $age = time() - filemtime($config["cache_file"]); if ($age < $config["cache_lifetime"]) { header("Content-Type: text/xml; charset=utf-8"); readfile($config["cache_file"]); exit; } } // ======================================== // FETCH JSON // ======================================== $ch = curl_init(); curl_setopt_array($ch, [ CURLOPT_URL => $config["mixins_json_url"], CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 30 ]); $json = curl_exec($ch); if (curl_errno($ch)) { logMessage("CURL ERROR: " . curl_error($ch)); if (file_exists($config["cache_file"])) { header("Content-Type: text/xml; charset=utf-8"); readfile($config["cache_file"]); exit; } die("Error fetching data"); } curl_close($ch); $products = json_decode($json, true); if (!is_array($products)) { logMessage("INVALID JSON"); if (file_exists($config["cache_file"])) { header("Content-Type: text/xml; charset=utf-8"); readfile($config["cache_file"]); exit; } die("Invalid data"); } // ======================================== // XML GENERATION // ======================================== $xml = new XMLWriter(); $xml->openMemory(); $xml->startDocument("1.0", "UTF-8"); $xml->startElement("products"); $count = 0; foreach ($products as $p) { if (($p["status"] ?? "") != "active") continue; if (($p["stock"] ?? 0) <= 0) continue; if (($p["price"] ?? 0) <= 0) continue; $id = $p["id"] ?? null; $name = $p["title"] ?? null; $price = intval($p["price"] ?? 0); $brand = $p["brand"] ?? ""; $category = $p["category"] ?? ""; $image = $p["image"] ?? ""; $url = $p["link"] ?? ""; if (!$id || !$name || !$url) continue; $xml->startElement("product"); $xml->writeElement("id", $id); $xml->writeElement("name", $name); $xml->writeElement("price", $price); $xml->writeElement("brand", $brand); $xml->writeElement("category", $category); $xml->writeElement("url", $url); $xml->writeElement("image", $image); $xml->writeElement("availability", "instock"); $xml->endElement(); $count++; } $xml->endElement(); $xml->endDocument(); $xml_output = $xml->outputMemory(); // ======================================== // SAVE CACHE // ======================================== file_put_contents($config["cache_file"], $xml_output); // ======================================== // LOG // ======================================== logMessage("Feed generated. Products: ".$count); // ======================================== // OUTPUT XML // ======================================== header("Content-Type: text/xml; charset=utf-8"); echo $xml_output; ?> /torob-feed/ torob.php cache.xml log.txt '; foreach($products as $p){ if($p['status'] != 'active' || $p['stock'] <= 0) continue; // فقط محصولات موجود $id = $p['id']; $name = htmlspecialchars($p['title']); $price = $p['price']; $brand = htmlspecialchars($p['brand']); $category = htmlspecialchars($p['category']); $image = $p['image']; $url = $p['link']; $xml .= " $id $name $price $brand $category $url $image instock "; } $xml .= ""; // ================================ // ذخیره فید محلی // ================================ file_put_contents($cache_file, $xml); // ================================ // ارسال خودکار به ترب // ================================ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $torob_api); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, ['feed_file' => new CURLFile($cache_file)]); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); curl_close($ch); // ================================ // لاگ آپدیت // ================================ file_put_contents(__DIR__ . '/torob-log.txt', date('Y-m-d H:i:s')." - Feed sent. Response: ".$response."\n", FILE_APPEND); echo "Feed sent to Torob successfully!"; ?>
جستجو در محصولات
کالایی در این صفحه موجود نیست
    پروفایلسبد خریددسته‌بندیخانه