Last active
October 5, 2017 15:21
-
-
Save acyuta/efc4b09bccabc3cf3e8ced40506a4e9c to your computer and use it in GitHub Desktop.
Yandex Xml Reporter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Быстрый отчёт по структуре товаров по XML от Yandex | |
* | |
* USING: php yml_parce.php "http://some.site/products_data_for_yandex.xml | |
* | |
* User: akim aka [email protected] | |
* Date: 10/5/17 | |
* Time: 9:27 PM | |
*/ | |
if (!isset($argv[1])) { | |
$url = "<!paste url here!>"; | |
} else { | |
$url = $argv[1]; | |
} | |
$data = file_get_contents($url); | |
$root = new SimpleXMLElement($data); | |
$shop = $root->shop; | |
$name = $shop->name; | |
$company = $shop->company; | |
$url = $shop->url; | |
echo "Названиe: $name\nКомпания: $company\nURL: $url\n\n"; | |
echo "Категорий " . $shop->categories->category->count() . " штук(-и)\n"; | |
echo "Продуктов " . $shop->offers->offer->count() . " штук(-и)\n"; | |
$report = []; | |
foreach ($shop->offers->offer as $offer) { | |
/* @var $offer SimpleXMLElement */ | |
foreach ($offer->children() as $child) { | |
/* @var $child SimpleXMLElement */ | |
$n = $child->getName(); | |
switch ($n) { | |
case 'param': | |
$pname = $child->attributes()->name->__toString(); | |
if (!isset($report['params'])) { | |
$report['params'] = []; | |
} | |
$val = $child->__toString(); | |
if (!isset($report['params'][$pname]) && !empty($val)) { | |
$report['params'][$pname] = $val; | |
} | |
break; | |
default: | |
if (!isset($report[$n])) { | |
$report[$n] = $child->__toString(); | |
} | |
break; | |
} | |
} | |
} | |
if (function_exists('yaml_emit')) | |
$result = yaml_emit($report, YAML_UTF8_ENCODING); | |
else | |
$result = var_export($report); | |
echo $result; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment