Skip to content

Instantly share code, notes, and snippets.

@sanmai
Last active August 16, 2023 12:53
Show Gist options
  • Save sanmai/2ed1ac19ac25ea27f3aa to your computer and use it in GitHub Desktop.
Save sanmai/2ed1ac19ac25ea27f3aa to your computer and use it in GitHub Desktop.
Генератор данных по товарам в формате YML
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE yml_catalog SYSTEM "shops.dtd">
<yml_catalog date="2016-03-14 18:03">
<shop>
<name>Shop Name</name>
<company>Company Name</company>
<url>https://www.example.com/</url>
<currencies>
<currency id="RUR" rate="1"/>
<currency id="USD" rate="81.00"/>
</currencies>
<categories>
<category id="1">Товары для дома</category>
</categories>
<delivery-options>
<option cost="0" days="17-20"/>
</delivery-options>
<cpa>0</cpa>
<offers>
<offer id="111122" available="true">
<url>http://example.com/shop/12348</url>
<price>30</price>
<oldprice>35</oldprice>
<currencyId>USD</currencyId>
<categoryId>1</categoryId>
<picture>http://example.com/img/shop/12348.jpg</picture>
<store>false</store>
<pickup>false</pickup>
<delivery>true</delivery>
<name>Наручные часы Casio A1234567B</name>
<vendor>Casio</vendor>
<description>Изящные наручные часы.</description>
<manufacturer_warranty>true</manufacturer_warranty>
<country_of_origin>Япония</country_of_origin>
<barcode>0123456789012</barcode>
</offer>
<offer id="444422" available="true">
<url>http://example.com/shop/12348</url>
<price>30</price>
<oldprice>35</oldprice>
<currencyId>USD</currencyId>
<categoryId>1</categoryId>
<picture>http://example.com/img/shop/12348.jpg</picture>
<store>false</store>
<pickup>false</pickup>
<delivery>true</delivery>
<name>Наручные часы Casio A1234567B</name>
<vendor>Casio</vendor>
<description>Изящные наручные часы.</description>
<manufacturer_warranty>true</manufacturer_warranty>
<country_of_origin>Япония</country_of_origin>
<barcode>0123456789012</barcode>
</offer>
</offers>
</shop>
</yml_catalog>
<?php
header("Content-Type: application/rss+xml; charset=utf-8");
/*
Описание формата:
https://yandex.ru/support/partnermarket/yml/about-yml.xml
https://yandex.ru/support/webmaster/goods-prices/technical-requirements.xml
https://webmaster.yandex.ru/xsdtest.xml?xsdvalue=10
Если у вас есть актуальный shops.dtd, сверить с ним можно так:
php yml_catalog.php | xmllint --dtdvalid shops.dtd --encode utf-8 -
*/
$implementation = new DOMImplementation();
$dtd = $implementation->createDocumentType('yml_catalog', null, 'shops.dtd');
$xml = $implementation->createDocument("", "", $dtd);
$xml->encoding = "UTF-8";
$xml->formatOutput = true;
$yml_catalog = $xml->createElement("yml_catalog");
$yml_catalog->setAttribute("date", date("Y-m-d H:m"));
$xml->appendChild($yml_catalog);
$shop = $xml->createElement("shop");
$yml_catalog->appendChild($shop);
$shop->appendChild($xml->createElement("name", "Shop Name"));
$shop->appendChild($xml->createElement("company", "Company Name"));
$shop->appendChild($xml->createElement("url", "https://www.example.com/"));
/*
* Курсы валют
*/
$currencies = $xml->createElement("currencies");
$shop->appendChild($currencies);
$currency = $xml->createElement("currency");
$currency->setAttribute("id", "RUR");
$currency->setAttribute("rate", "1");
$currencies->appendChild($currency);
$currency = $xml->createElement("currency");
$currency->setAttribute("id", "USD");
$currency->setAttribute("rate", "81.00");
$currencies->appendChild($currency);
/*
* Разделы каталога
*/
$categories = $xml->createElement("categories");
$shop->appendChild($categories);
$category = $xml->createElement("category", "Товары для дома");
$category->setAttribute("id", "1");
$categories->appendChild($category);
/*
* Варианты доставки
*/
$deliveryOptions = $xml->createElement("delivery-options");
$shop->appendChild($deliveryOptions);
$option = $xml->createElement("option");
$option->setAttribute("cost", "0");
$option->setAttribute("days", "17-20");
$deliveryOptions->appendChild($option);
/*
* CPA
*/
$cpa = $xml->createElement("cpa","0");
$shop->appendChild($cpa);
/*
* Товарные предложения
*/
$offers = $xml->createElement("offers");
$shop->appendChild($offers);
/*
* Отдельный товар
*/
$offer = $xml->createElement("offer");
$offer->setAttribute("id", "111122");
$offer->setAttribute("available", "true");
$offers->appendChild($offer);
$offer->appendChild($xml->createElement("url", "http://example.com/shop/12348"));
$offer->appendChild($xml->createElement("price", "30"));
$offer->appendChild($xml->createElement("oldprice", "35"));
$offer->appendChild($xml->createElement("currencyId", "USD"));
$offer->appendChild($xml->createElement("categoryId", "1"));
$offer->appendChild($xml->createElement("picture", "http://example.com/img/shop/12348.jpg"));
$offer->appendChild($xml->createElement("store", "false"));
$offer->appendChild($xml->createElement("pickup", "false"));
$offer->appendChild($xml->createElement("delivery", "true"));
$offer->appendChild($xml->createElement("name", "Наручные часы Casio A1234567B"));
$offer->appendChild($xml->createElement("vendor", "Casio"));
$offer->appendChild($xml->createElement("description", "Изящные наручные часы.")); // длина не более 175 символов, без HTML-теги
$offer->appendChild($xml->createElement("manufacturer_warranty", "true"));
$offer->appendChild($xml->createElement("country_of_origin", "Япония"));
$offer->appendChild($xml->createElement("barcode", "0123456789012"));
/*
* Ещё один товар
*/
$offer = $xml->createElement("offer");
$offer->setAttribute("id", "444422");
$offer->setAttribute("available", "true");
$offers->appendChild($offer);
$offer->appendChild($xml->createElement("url", "http://example.com/shop/12348"));
$offer->appendChild($xml->createElement("price", "30"));
$offer->appendChild($xml->createElement("oldprice", "35"));
$offer->appendChild($xml->createElement("currencyId", "USD"));
$offer->appendChild($xml->createElement("categoryId", "1"));
$offer->appendChild($xml->createElement("picture", "http://example.com/img/shop/12348.jpg"));
$offer->appendChild($xml->createElement("store", "false"));
$offer->appendChild($xml->createElement("pickup", "false"));
$offer->appendChild($xml->createElement("delivery", "true"));
$offer->appendChild($xml->createElement("name", "Наручные часы Casio A1234567B"));
$offer->appendChild($xml->createElement("vendor", "Casio"));
$offer->appendChild($xml->createElement("description", "Изящные наручные часы.")); // длина не более 175 символов, без HTML-теги
$offer->appendChild($xml->createElement("manufacturer_warranty", "true"));
$offer->appendChild($xml->createElement("country_of_origin", "Япония"));
$offer->appendChild($xml->createElement("barcode", "0123456789012"));
echo $xml->saveXML();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment