Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- ignore_user_abort(1);
- error_reporting(0);
- $url = 'http://fireboxshop.com/link/62b96bf36e247093147fb928638f5c70308f946f.xml';
- $file_path = dirname( $_SERVER['SCRIPT_FILENAME'] ) . '/';
- $file_name = 'download.xml';
- unlink($file_name);
- unlink('new.csv');
- /* Скачиваем xml */
- $ch = curl_init($url);
- $fp = fopen($file_path . $file_name, 'wb');
- curl_setopt($ch, CURLOPT_FILE, $fp);
- // curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION , true);
- curl_exec($ch);
- curl_close($ch);
- // var_dump($res);
- fclose($fp);
- /* Функция отдачи конечного файла пользователю */
- function file_download($file) {
- if (file_exists($file)) {
- if (ob_get_level()) {
- ob_end_clean();
- }
- header('Content-Description: File Transfer');
- header('Content-Type: application/octet-stream');
- header('Content-Disposition: attachment; filename=' . basename($file));
- header('Content-Transfer-Encoding: binary');
- header('Access-Control-Allow-Origin: *');
- header('Expires: 0');
- header('Cache-Control: must-revalidate');
- header('Pragma: public');
- header('Content-Length: ' . filesize($file));
- if ($fd = fopen($file, 'rb')) {
- while (!feof($fd)) {
- print fread($fd, 1024);
- }
- fclose($fd);
- }
- return true;
- }else {
- return false;
- }
- }
- /* Формируем категории */
- $cat = array();
- $xml = new XMLReader();
- $xml->open($file_name);
- while($xml->read() && $xml->name !== 'category');
- while($xml->name === 'category'){
- $node = new SimpleXMLElement($xml->readOuterXML());
- $id = +$xml->getAttribute("id");
- $parent_id = +$xml->getAttribute("parentId");
- $name = ''. $node;
- $parent_name = ($parent_id != 0 ? $cat[$parent_id][0] : false);
- $cat[$id] = [$name, $parent_name];
- $xml->next('category');
- }
- $xml->close();
- /* Формируем параметры */
- $xml = new XMLReader();
- $xml->open($file_name);
- $allparams = array();
- $idparams = array();
- while($xml->read() && $xml->name !== 'offer');
- while($xml->name === 'offer'){
- $node = new SimpleXMLElement($xml->readOuterXML());
- $id = +$xml->getAttribute("id");
- foreach ($node->param as $param){
- if ($param['name'] != 'ПодКатегория' ) {
- $prefix = $param['name'] == 'Размер' ? 'Свойство: ' : 'Параметр: ';
- $name = ''. $prefix . $param['name'];
- $value = ''. $param;
- $allparams[] = ''. $prefix . $param['name'];
- $idparams[$id][$name] = $value;
- }
- }
- $xml->next('offer');
- }
- $xml->close();
- $allparams = array_unique($allparams);
- sort($allparams);
- /* Формируем csv */
- $xml = new XMLReader();
- $xml->open($file_name);
- $flag = true;
- while($xml->read() && $xml->name !== 'offer');
- while($xml->name === 'offer'){
- $node = new SimpleXMLElement($xml->readOuterXML());
- $available = $xml->getAttribute("available");
- if ($available === 'true') {
- $id = $xml->getAttribute("id");
- $name = $node->name;
- $price = $node->price;
- $vendor = $node->vendor;
- $vendorCode = $node->vendorCode;
- $description = $node->description;
- $c = +$node->categoryId;
- if ($cat[$c][1] != false) {
- $parent_category = $cat[$c][1];
- $category = $cat[$c][0];
- } else {
- $parent_category = $cat[$c][0];
- $category = '';
- }
- $picture = '';
- foreach ($node->picture as $pic){
- $picture .= $pic .' ';
- }
- if($flag){
- $str = '"Корневая";"Подкатегория 1";"Артикул";"Название товара";"Полное описание";"Изображения";"Цена продажи";"Параметр: Производитель";';
- $j = count($allparams);
- while($j-- > 0){
- $str .= '"'. $allparams[$j] .'";';
- }
- $str .= PHP_EOL;
- $str = mb_convert_encoding ($str , "Windows-1251", "UTF-8");
- $path = fopen("new.csv", "a+");
- fwrite($path, $str);
- fclose($path);
- $flag = false;
- }
- $params = '';
- $j = count($allparams);
- while($j-- > 0){
- $params .= '"'. $idparams[$id][$allparams[$j]] .'";';
- }
- $str = '"'. $parent_category .'";';
- $str .= '"'. $category .'";';
- $str .= '"'. $vendorCode .'";';
- $str .= '"'. $name .'";';
- $str .= '"'. $description .'";';
- $str .= '"'. $picture .'";';
- $str .= '"'. $price .'";';
- $str .= '"'. $vendor .'";';
- $str .= $params;
- $str .= PHP_EOL;
- $str = mb_convert_encoding ($str ,"Windows-1251", "UTF-8");
- $path = fopen("new.csv", "a+");
- fwrite($path, $str);
- fclose($path);
- }
- $xml->next('offer');
- }
- $xml->close();
- gc_enable();
- $file = "new.csv";
- file_download($file);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement