<?php
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 1);
$config = include 'config.php';
$host = $config['db']['host'];
$u_name = $config['db']['user'];
$pass = $config['db']['pass'];
$db_name = $config['db']['database'];
$title = "Sitename";
$description = 'sitename - ';
$url = ((!empty($_SERVER['HTTPS'])) ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'].'/';
$link = $url."rss.xml";
$path_media='media/images_boards/big/';
//Функция извлечения одной картинки из строки
function img_name($str){
//$str = '["610fb11d195ad.jpg","610fb11d2dda4.jpg","610fb11d495cc.jpg"]';
$vowels = array('[', ']', '"');
$str = str_replace($vowels, "", $str);
$fl = explode(",", $str);
return $fl[0];
}
$xw = xmlwriter_open_memory();
xmlwriter_set_indent($xw, 1);
$res = xmlwriter_set_indent_string($xw, ' ');
xmlwriter_start_document($xw, '1.0', 'UTF-8');
// Корневой элемент RSS
xmlwriter_start_element($xw, 'rss');
// Атрибуты элемента rss
xmlwriter_start_attribute($xw, 'version');
xmlwriter_text($xw, '2.0');
xmlwriter_end_attribute($xw);
xmlwriter_start_attribute($xw, 'xmlns:content');
xmlwriter_text($xw, 'http://purl.org/rss/1.0/modules/content/');
xmlwriter_end_attribute($xw);
xmlwriter_start_attribute($xw, 'xmlns:wfw');
xmlwriter_text($xw, 'http://wellformedweb.org/CommentAPI/');
xmlwriter_end_attribute($xw);
xmlwriter_start_attribute($xw, 'xmlns:dc');
xmlwriter_text($xw, 'http://purl.org/dc/elements/1.1/');
xmlwriter_end_attribute($xw);
xmlwriter_start_attribute($xw, 'xmlns:atom');
xmlwriter_text($xw, 'http://www.w3.org/2005/Atom');
xmlwriter_end_attribute($xw);
xmlwriter_start_attribute($xw, 'xmlns:sy');
xmlwriter_text($xw, 'http://purl.org/rss/1.0/modules/syndication/');
xmlwriter_end_attribute($xw);
xmlwriter_start_attribute($xw, 'xmlns:slash');
xmlwriter_text($xw, 'http://purl.org/rss/1.0/modules/slash/');
xmlwriter_end_attribute($xw);
xmlwriter_start_attribute($xw, 'xmlns:feedburner');
xmlwriter_text($xw, 'http://rssnamespace.org/feedburner/ext/1.0');
xmlwriter_end_attribute($xw);
xmlwriter_start_attribute($xw, 'xmlns:media');
xmlwriter_text($xw, 'http://search.yahoo.com/mrss/');
xmlwriter_end_attribute($xw);
xmlwriter_start_attribute($xw, 'xmlns:snf');
xmlwriter_text($xw, 'http://www.smartnews.be/snf');
xmlwriter_end_attribute($xw);
// Создаём дочерний элемент Channel
xmlwriter_start_element($xw, 'channel');
// Создаём дочерний элемент Title
xmlwriter_start_element($xw, 'title');
xmlwriter_text($xw, $title);
xmlwriter_end_element($xw); // title
// Создаём дочерний элемент Link
xmlwriter_start_element($xw, 'link');
xmlwriter_text($xw, $link);
xmlwriter_end_element($xw); // link
// Создаём дочерний элемент description
xmlwriter_start_element($xw, 'description');
xmlwriter_text($xw, $description);
xmlwriter_end_element($xw); // description
// Создаём дочерний элемент lastBuildDate
xmlwriter_start_element($xw, 'lastBuildDate');
xmlwriter_text($xw, gmdate('D, d M Y H:i:s', strtotime(date('Y-m-d'))). ' GMT');
xmlwriter_end_element($xw); // lastBuildDate
// Создаём дочерний элемент sy:updatePeriod
xmlwriter_start_element($xw, 'sy:updatePeriod');
xmlwriter_text($xw, 'hourly');
xmlwriter_end_element($xw); // sy:updatePeriod
// Создаём дочерний элемент sy:updateFrequency
xmlwriter_start_element($xw, 'sy:updateFrequency');
xmlwriter_text($xw, 1);
xmlwriter_end_element($xw); // sy:updateFrequency
//Работаем с БД
$link = mysqli_connect($host, $u_name, $pass, $db_name);
if ($link == false){
print("Ошибка: Невозможно подключиться к MySQL " . mysqli_connect_error());
}
mysqli_query($link,"SET NAMES 'utf8'");
mysqli_query($link,"SET CHARACTER SET 'utf8'");
mysqli_query($link,"SET SESSION collation_connection = 'utf8_general_ci'");
$sql = 'SELECT `ads_id`,`ads_title`,`ads_alias`,`ads_text`,`ads_id_cat`,`ads_datetime_add`,`ads_images`,`ads_price`, `ads_currency`, `ads_city_id` FROM `uni_ads` where`ads_status`=1 AND `ads_period_publication`>CURRENT_TIMESTAMP() ORDER BY ads_id DESC LIMIT 0,200';
$result = mysqli_query($link, $sql);
while ($row = mysqli_fetch_array($result)) {
//составляем ссылку на объявлене
$post_url='';
$sql2 = 'SELECT * FROM `uni_city` WHERE `city_id`='.$row['ads_city_id'];
$result2 = mysqli_query($link, $sql2);
$result2 = mysqli_fetch_array($result2);
$city=$result2['city_alias'];
$sql2 = 'SELECT * FROM `uni_category_board` WHERE `category_board_id`='.$row['ads_id_cat'];
$result2 = mysqli_query($link, $sql2);
$result2 = mysqli_fetch_array($result2);
$cat=$result2['category_board_alias'];
$post_url=$city.'/'.$cat.'/'.$row['ads_alias'].'-'.$row['ads_id'];
//запоняем постами
// Создаём дочерний элемент item
xmlwriter_start_element($xw, 'item');
xmlwriter_start_attribute($xw, 'turbo');
xmlwriter_text($xw, 'true');
xmlwriter_end_attribute($xw);
// Создаём дочерний элемент title
xmlwriter_start_element($xw, 'title');
xmlwriter_text($xw, $row['ads_title']);
xmlwriter_end_element($xw); // title
// Создаём дочерний элемент link
xmlwriter_start_element($xw, 'link');
xmlwriter_text($xw, $url.$post_url);
xmlwriter_end_element($xw); // link
// content:encoded
xmlwriter_start_element($xw, 'content:encoded');
xmlwriter_write_cdata($xw, $row['ads_text']."<br><br> Цена: ".$row['ads_price'].$row['ads_currency']);
xmlwriter_end_cdata($xw);
xmlwriter_end_element($xw); // content:encoded
// Создаём дочерний элемент description
xmlwriter_start_element($xw, 'description');
xmlwriter_text($xw, $row['ads_text']."\n\n Цена: ".$row['ads_price'].$row['ads_currency']);
xmlwriter_end_element($xw); // description
// Создаём дочерний элемент guid
xmlwriter_start_element($xw, 'guid');
xmlwriter_start_attribute($xw, 'isPermaLink');
xmlwriter_text($xw, 'true');
xmlwriter_end_attribute($xw);
xmlwriter_text($xw, $url.$post_url);
xmlwriter_end_element($xw); // guid
// Создаём дочерний элемент pubDate
xmlwriter_start_element($xw, 'pubDate');
xmlwriter_text($xw, gmdate('D, d M Y H:i:s', strtotime($row['ads_datetime_add'])). ' GMT');
xmlwriter_end_element($xw); // pubDate
// Создаём дочерний элемент media:thumbnail
xmlwriter_start_element($xw, 'media:thumbnail');
xmlwriter_start_attribute($xw, 'url');
//xmlwriter_text($xw, $url.$path_media);
xmlwriter_text($xw, $url.$path_media.img_name($row['ads_images']));
xmlwriter_end_attribute($xw);
xmlwriter_end_element($xw); // media:thumbnail
xmlwriter_end_element($xw); // item
}
xmlwriter_end_element($xw); // Channel
xmlwriter_end_element($xw); // RSS
xmlwriter_end_document($xw);
header("Content-Type: text/xml");
echo xmlwriter_output_memory($xw);