core/parcer/virtual/1/nix.php

220 lines
5.1 KiB
PHP
Executable File

<?php
ini_set( 'display_errors', 0 );
$_SERVER['SERVER_NAME']='v.yurecnt.ru';
require_once('/home/cloud/core/set/v.yurecnt.ru.php');
require_once('/home/cloud/core/api/php/db.php');
require_once('/home/cloud/core/api/php/json.php');
function html_to_obj($html) {
$dom = new DOMDocument();
// $dom->loadHTML($html);
@$dom->loadHTML(mb_convert_encoding(@$html, 'HTML-ENTITIES', 'UTF-8'));
return element_to_obj($dom->documentElement);
}
function element_to_obj($element) {
@$obj = array( "tag" => @$element->tagName );
foreach (@$element->attributes as $attribute) {
$obj[$attribute->name] = $attribute->value;
}
foreach ($element->childNodes as $subElement) {
if ($subElement->nodeType == XML_TEXT_NODE) {
$obj["html"] = $subElement->wholeText;
}
else {
$obj["children"][] = element_to_obj($subElement);
}
}
return $obj;
}
function generate_password($number)
{$arr = array('a','b','c','d','e','f', 'g','h','i','j','k','l','m','n','o','p','r','s','t','u','v','x','y','z','1','2','3','4','5','6','7','8','9','0');
$pass = "";
for($i = 0; $i < $number; $i++){
$index = rand(0, count($arr) - 1);
$pass .= $arr[$index];}
return $pass;}
$a=file_get_contents('nix.xml');
/*
$xml = simplexml_load_string($a, "SimpleXMLElement", LIBXML_NOCDATA);
$json = json_encode($xml);
$array = json_decode($json,TRUE);
print_r ($array);
*/
function XMLtoArray($xml) {
$previous_value = libxml_use_internal_errors(true);
$dom = new DOMDocument('1.0', 'UTF-8');
$dom->preserveWhiteSpace = false;
$dom->loadXml($xml);
libxml_use_internal_errors($previous_value);
if (libxml_get_errors()) {
return [];
}
return DOMtoArray($dom);
}
function DOMtoArray($root) {
$result = array();
if ($root->hasAttributes()) {
$attrs = $root->attributes;
foreach ($attrs as $attr) {
$result['@attributes'][$attr->name] = $attr->value;
}
}
if ($root->hasChildNodes()) {
$children = $root->childNodes;
if ($children->length == 1) {
$child = $children->item(0);
if (in_array($child->nodeType,[XML_TEXT_NODE,XML_CDATA_SECTION_NODE])) {
$result['_value'] = $child->nodeValue;
return count($result) == 1
? $result['_value']
: $result;
}
}
$groups = array();
foreach ($children as $child) {
if (!isset($result[$child->nodeName])) {
$result[$child->nodeName] = DOMtoArray($child);
} else {
if (!isset($groups[$child->nodeName])) {
$result[$child->nodeName] = array($result[$child->nodeName]);
$groups[$child->nodeName] = 1;
}
$result[$child->nodeName][] = DOMtoArray($child);
}
}
}
return $result;
}
$xml = $a;
$b=XMLtoArray($xml);
$tovar=$b['yml_catalog'][1]['shop']['offers']['offer'];
for ($i=0; $i<count($tovar); $i++){
unset($artikul);
unset($id_tovar);
$artikul=$tovar[$i]['@attributes']['id'];
$id_tovar=DB::getValue("SELECT `id` FROM `tovar` WHERE `artikul`=? LIMIT 1", $artikul);
if ($id_tovar){
//Собираем характеристики
$hars=$tovar[$i]['param'];
for ($j=0;$j<count($hars);$j++){
$tip_har = $hars[$j]['@attributes']['name'];
$har = $hars[$j]['_value'];
echo $tip_har . "\n";
$id_sp_har=DB::getValue("SELECT `id` FROM `tovar_har_sp` WHERE `txt`=? LIMIT 1", $tip_har);
if (!$id_sp_har)$id_sp_har = DB::add("INSERT INTO `tovar_har_sp` (`txt`) VALUES (?)", $tip_har);
echo $id_sp_har . "\n";
DB::add("INSERT INTO `tovar_har` (`tovar_har_sp_id`, `tovar_id`, `txt`) VALUES (?, ?, ?)",
array(
$id_sp_har,
$id_tovar, $har
)
);
}
}
unset($hars);
unset($tip_har);
unset($har);
}
//print_r($hars);
/*#print_r(html_to_obj($a['children'][0]['children'][0])); 337854
$b=html_to_obj($a);
//print_r($b['children'][0]['children'][0]['children'][0]['children'][5]['children']);
$tovar_massiv=$b['children'][0]['children'][0]['children'][0]['children'][5]['children'];
for ($i=0; $i<count($tovar_massiv); $i++){
$artikul=$tovar_massiv[$i]['id'];
$id_tovar=DB::getValue("SELECT `id` FROM `tovar` WHERE `artikul`=?", $artikul);
if ($id_tovar){
//Находим и удаляем картинки.....
DB::set("DELETE FROM `tovar_img` WHERE `tovar_id`=?", $id_tovar);
$f_pic=$tovar_massiv[$i]['children'];
for ($j=0; $j<count($f_pic); $j++){
$tip=$f_pic[$j]['tag'];
if ($tip=='picture'){
$link[$i][$j]=$f_pic[$j]['html'];
$filename[$i][$j] = generate_password(30);
exec("wget " . $link[$i][$j] . " -O /home/cloud/core/img/o.yurecnt.ru/tovar/" . $filename[$i][$j] . ".jpg");
DB::add("INSERT INTO `tovar_img` (`filename`, `tovar_id`) VALUES (?, ?)", array( $filename[$i][$j], $id_tovar ) );
}
}
}
//echo $artikul;
}
*/
?>