Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2020
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.04 KB | None | 0 0
  1. <?php
  2. ignore_user_abort(1);
  3. error_reporting(0);
  4.  
  5.  
  6. $url = 'http://fireboxshop.com/link/62b96bf36e247093147fb928638f5c70308f946f.xml';
  7.  
  8. $file_path = dirname( $_SERVER['SCRIPT_FILENAME'] ) . '/';
  9. $file_name = 'download.xml';
  10. unlink($file_name);
  11. unlink('new.csv');
  12.  
  13. /* Скачиваем xml */
  14. $ch = curl_init($url);
  15. $fp = fopen($file_path . $file_name, 'wb');
  16. curl_setopt($ch, CURLOPT_FILE, $fp);
  17. // curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  18. curl_setopt($ch, CURLOPT_HEADER, 0);
  19. curl_setopt($ch, CURLOPT_FOLLOWLOCATION , true);
  20. curl_exec($ch);
  21. curl_close($ch);
  22. // var_dump($res);
  23. fclose($fp);
  24.  
  25.  
  26. /* Функция отдачи конечного файла пользователю */
  27. function file_download($file) {
  28.   if (file_exists($file)) {
  29.     if (ob_get_level()) {
  30.       ob_end_clean();
  31.     }
  32.  
  33.     header('Content-Description: File Transfer');
  34.     header('Content-Type: application/octet-stream');
  35.     header('Content-Disposition: attachment; filename=' . basename($file));
  36.     header('Content-Transfer-Encoding: binary');
  37.     header('Access-Control-Allow-Origin: *');
  38.     header('Expires: 0');
  39.     header('Cache-Control: must-revalidate');
  40.     header('Pragma: public');
  41.     header('Content-Length: ' . filesize($file));
  42.    
  43.     if ($fd = fopen($file, 'rb')) {
  44.       while (!feof($fd)) {
  45.         print fread($fd, 1024);
  46.       }
  47.       fclose($fd);
  48.     }
  49.  
  50.     return true;
  51.   }else {
  52.     return false;
  53.   }
  54. }
  55.  
  56.  
  57. /* Формируем категории */
  58.  
  59.   $cat = array();
  60.   $xml = new XMLReader();
  61.   $xml->open($file_name);
  62.   while($xml->read() && $xml->name !== 'category');
  63.     while($xml->name === 'category'){
  64.       $node = new SimpleXMLElement($xml->readOuterXML());
  65.       $id = +$xml->getAttribute("id");
  66.       $parent_id = +$xml->getAttribute("parentId");
  67.       $name = ''. $node;
  68.       $parent_name = ($parent_id != 0 ? $cat[$parent_id][0] : false);
  69.       $cat[$id] = [$name, $parent_name];
  70.      
  71.     $xml->next('category');
  72.     }
  73.   $xml->close();   
  74.  
  75.  
  76. /* Формируем параметры */  
  77.   $xml = new XMLReader();
  78.   $xml->open($file_name);      
  79.   $allparams = array();
  80.   $idparams = array();
  81.   while($xml->read() && $xml->name !== 'offer');
  82.     while($xml->name === 'offer'){
  83.       $node = new SimpleXMLElement($xml->readOuterXML());  
  84.       $id = +$xml->getAttribute("id");
  85.      
  86.       foreach ($node->param as $param){
  87.           if ($param['name'] != 'ПодКатегория' ) {
  88.               $prefix = $param['name'] == 'Размер' ? 'Свойство: ' : 'Параметр: ';
  89.         $name = ''. $prefix . $param['name'];
  90.         $value = ''. $param;
  91.         $allparams[] = ''. $prefix . $param['name'];               
  92.         $idparams[$id][$name] = $value;
  93.         }
  94.       }            
  95.     $xml->next('offer');
  96.     }
  97.   $xml->close();
  98.   $allparams = array_unique($allparams);
  99.   sort($allparams);
  100.  
  101. /* Формируем csv */  
  102.   $xml = new XMLReader();
  103.   $xml->open($file_name);
  104.   $flag = true;
  105.   while($xml->read() && $xml->name !== 'offer');
  106.     while($xml->name === 'offer'){
  107.       $node = new SimpleXMLElement($xml->readOuterXML());
  108.       $available = $xml->getAttribute("available");
  109.       if ($available === 'true') {
  110.       $id = $xml->getAttribute("id");
  111.       $name = $node->name;
  112.       $price = $node->price;
  113.       $vendor = $node->vendor;
  114.       $vendorCode = $node->vendorCode;
  115.       $description = $node->description;
  116.       $c = +$node->categoryId;
  117.       if ($cat[$c][1] != false) {
  118.         $parent_category = $cat[$c][1];
  119.         $category = $cat[$c][0];   
  120.       } else {
  121.         $parent_category = $cat[$c][0];
  122.         $category = '';
  123.       }
  124.      
  125.       $picture = '';
  126.       foreach ($node->picture as $pic){
  127.         $picture .= $pic .' ';             
  128.       }                
  129.      
  130.       if($flag){
  131.         $str = '"Корневая";"Подкатегория 1";"Артикул";"Название товара";"Полное описание";"Изображения";"Цена продажи";"Параметр: Производитель";';
  132.         $j = count($allparams);
  133.         while($j-- > 0){
  134.           $str .= '"'. $allparams[$j] .'";';
  135.         }
  136.         $str .= PHP_EOL;
  137.         $str = mb_convert_encoding ($str , "Windows-1251", "UTF-8");           
  138.         $path = fopen("new.csv", "a+");
  139.         fwrite($path, $str);
  140.         fclose($path);
  141.         $flag = false;
  142.       }
  143.      
  144.       $params = '';
  145.       $j = count($allparams);
  146.       while($j-- > 0){             
  147.         $params .= '"'. $idparams[$id][$allparams[$j]] .'";';          
  148.       }    
  149.       $str = '"'. $parent_category .'";';
  150.       $str .= '"'. $category .'";';
  151.       $str .= '"'. $vendorCode .'";';
  152.       $str .= '"'. $name .'";';
  153.       $str .= '"'. $description .'";';
  154.       $str .= '"'. $picture .'";';
  155.       $str .= '"'. $price .'";';
  156.       $str .= '"'. $vendor .'";';
  157.       $str .= $params;
  158.       $str .= PHP_EOL;
  159.      
  160.       $str = mb_convert_encoding ($str ,"Windows-1251", "UTF-8");      
  161.      
  162.       $path = fopen("new.csv", "a+");
  163.       fwrite($path, $str);
  164.       fclose($path);       
  165.       }
  166.     $xml->next('offer');
  167.     }
  168.   $xml->close();
  169.  
  170. gc_enable();
  171. $file = "new.csv";
  172. file_download($file);
  173.  
  174. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement