Skip to content

Instantly share code, notes, and snippets.

@sanmai
Created March 2, 2016 02:02
Show Gist options
  • Select an option

  • Save sanmai/cc01253480ab05432ad8 to your computer and use it in GitHub Desktop.

Select an option

Save sanmai/cc01253480ab05432ad8 to your computer and use it in GitHub Desktop.
Генератор валидного RSS
<?php
header('Content-Type: application/rss+xml; charset=utf-8');
$self = "https://www.example.com/news.xml";
$url = "https://www.example.com/";
$name = "Новости Example.com";
$xml = new DOMDocument("1.0", "utf-8");
$xml->formatOutput = true;
$rss = $xml->createElement("rss");
$rss->setAttribute("version", "2.0");
$rss->setAttribute("xmlns:atom", "http://www.w3.org/2005/Atom");
$xml->appendChild($rss);
$channel = $xml->createElement("channel");
$rss->appendChild($channel);
$atomLink = $xml->createElement("atom:link");
$atomLink->setAttribute("href", $self);
$atomLink->setAttribute("rel", "self");
$atomLink->setAttribute("type", "application/rss+xml");
$channel->appendChild($atomLink);
$channel->appendChild($xml->createElement("title", $name));
$channel->appendChild($xml->createElement("link", $url));
$channel->appendChild($xml->createElement("description", $name));
$channel->appendChild($xml->createElement("language", "ru"));
$channel->appendChild($xml->createElement("lastBuildDate", date("r")));
$channel->appendChild($xml->createElement("ttl", "60"));
$news = [
["Название новости", "https://www.example.com/news/123123", "2016-02-29 11:00:01", "<h1>Название</h1><p>Текст новости</p><a href=\"/profile?id=123\">Имя автора & профиль</a>"],
["Название новости", "https://www.example.com/news/123124", "2016-03-02 10:00:01", "<h1>Название</h1><p>Текст новости</p><a href=\"/profile?id=123\">Имя автора & профиль</a>"],
["Название новости", "https://www.example.com/news/123125", "2016-01-29 13:00:01", "<h1>Название</h1><p>Текст новости</p><a href=\"/profile?id=123\">Имя автора & профиль</a>"],
["Название новости", "https://www.example.com/news/123126", "2016-01-19 14:00:01", "<h1>Название</h1><p>Текст новости</p><a href=\"/profile?id=123\">Имя автора & профиль</a>"],
["Название новости", "https://www.example.com/news/123127", "2016-02-28 15:00:01", "<h1>Название</h1><p>Текст новости</p><a href=\"/profile?id=123\">Имя автора & профиль</a>"],
];
$maxDate = 0;
$entries = [];
foreach ($news as list ($title, $articleUrl, $date, $text))
{
$entry = $xml->createElement('item');
$entry->appendChild($xml->createElement('title', $title));
$entry->appendChild($xml->createElement('link', $articleUrl));
$entry->appendChild($xml->createElement('guid', $articleUrl));
$entry->appendChild($xml->createElement('pubDate',
date('r', strtotime($date))));
// сделаем абсолютные ссылки
$text = preg_replace('#(href=")/([^/])#us', "\\1$url\\2", $text);
$textElem = $xml->createElement('description');
$textElem->appendChild($xml->createTextNode($text));
$entry->appendChild($textElem);
$entries[] = $entry;
if ($date > $maxDate) {
$maxDate = $date;
}
}
$channel->appendChild($xml->createElement("pubDate",
date('r', strtotime($maxDate))));
foreach ($entries as $entry) {
$channel->appendChild($entry);
}
echo $xml->saveXML();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment