"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
🎁 کد تخفیف ویژه اولین خرید: naniwa | ارسال سریع به سراسر کشور 🚀.