Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- if(!isset($_GET["feed"])) {
- http_response_code(400);
- echo "<h1>400 Bad Request</h1>";
- echo "The request cannot be fulfilled due to lack of parameters.";
- exit(400);
- }
- $jsdt = json_decode($_GET["feed"], true);
- if(!isset($jsdt["type"]) | !isset($jsdt["url"])){
- http_response_code(400);
- echo "<h1>400 Bad Request</h1>";
- echo "The request cannot be fulfilled due to bad syntax.";
- exit(400);
- }
- $feed = file_get_contents($jsdt["url"]);
- switch ($jsdt["type"]) {
- case "atom":
- echo json_encode(parseatom(simplexml_load_string($feed)));
- break;
- case "rss2":
- echo json_encode(parserss2(simplexml_load_string($feed)));
- break;
- case "rss":
- echo json_encode(parserss(simplexml_load_string($feed)));
- break;
- case "rdf":
- echo json_encode(parserdf(simplexml_load_string($feed)));
- break;
- default:
- http_response_code(406);
- break;
- }
- //Парсинг ака ДИЧАЙШИЙ ГОВНОКОД
- function parseatom($xmlfeed)
- {
- $parsed = array();
- //Заголовок
- $parsed["title"] = (string) $xmlfeed->title;
- //Подзаголовок
- $parsed["desc"] = (string) $xmlfeed->subtitle;
- //Тип ленты, хз зачем, пусть будет
- $parsed["type"] = "atom";
- //Ссылки
- foreach ($xmlfeed->link as $link) { $parsed["links"][(string) $link["rel"]] = (string) $link["href"]; };
- //Сами новости
- if (!empty($xmlfeed->entry)) {
- foreach ($xmlfeed->entry as $entry) {
- //Категории
- $cats = array();
- foreach ($entry->category as $category) { $cats[] = (string) $category["term"]; };
- //Линки
- $entrylink = array();
- foreach ($entry->link as $link) { $entrylink[(string) $link["rel"]] = (string) $link["href"]; };
- //Проверка на краткое описание
- if (isset($entry->summary)) {$desc = (string) $entry->summary;}
- else $desc = "";
- //Ну, пошло-поехало
- $parsed["entries"][] = array(
- //Заголовок
- "title" => (string) $entry->title,
- //Краткое описание
- "desc" => $desc,
- //Категории
- "categories" => $cats,
- //Дата-время
- "date" => date('Y-m-d h:i:s A', strtotime((string) $entry->published)),
- //Линки
- "links" => $entrylink,
- //Автор
- "author" => array(
- "name" => (string) $entry->author->name,
- "url" => (string) $entry->author->uri
- ),
- //Содержимое
- "content" => (string) $entry->content
- );
- };
- };
- return $parsed;
- };
- //ЭТУ ЕБАНИНУ Я ЕЩЕ НА РАБОТОСПОСОБНОСТЬ НЕ ПРОВЕРЯЛ
- function parserss2($xmlfeed) {
- $parsed = array();
- //Заголовок
- $parsed["title"] = (string) $xmlfeed->channel->title;
- //Подзаголовок
- $parsed["desc"] = (string) $xmlfeed->channel->description;
- //Тип ленты, хз зачем, пусть будет
- $parsed["type"] = "rss2";
- //Ссылки
- $parsed["links"] = array( "link" => (string) $xmlfeed->channel->link );
- //ЭТО СДЕЛАНО СПЕЦИАЛЬНО! Иначе зацикливается к ебеням на первой ячейке!!!!!!!
- $namespaces = $xmlfeed->getNamespaces(true);
- //Даже не спрашивайте, что это, ок?
- foreach ($namespaces as $namespace => $namespaceValue) { $xmlfeed->registerXPathNamespace($namespace, $namespaceValue); };
- //Сами новости
- if (!empty($xmlfeed->channel->item)) {
- foreach ($xmlfeed->channel->item as $entry) {
- //Категории
- $cats = array();
- foreach ($entry->children() as $child) { if ($child->getName() == "category") { $cats[] = (string) $child; } };
- //Автор
- if (!empty($namespaces["dc"]) && $creator = $item->xpath("dc:creator")) { $author = (string) $creator[0]; }
- else { $author = ""; };
- //Содержимое
- if (!empty($namespaces["encoded"]) && $encoded = $item->xpath("content:encoded")) { $content = (string) $encoded[0]; }
- else {$content = "";};
- //Проверка на краткое описание
- if (isset($entry->description)) {$desc = (string) $entry->description;}
- else $desc = "";
- //Ну, пошло-поехало
- $parsed["entries"][] = array(
- //Заголовок
- "title" => (string) $entry->title,
- //Краткое описание
- "desc" => $desc,
- //Категории
- "categories" => $cats,
- //Дата-время
- "date" => date('Y-m-d h:i:s A', strtotime((string) $entry->pubDate)),
- //Линки
- "links" => array( "link" => (string) $entry->link ),
- //Автор
- "author" => array( "name" => $author ),
- //Содержимое
- "content" => (string) $entry->content
- );
- };
- };
- return $parsed;
- };
- //А теперь старый добрый RSS первой версии без неведомой ебанины, в которую его превратили во второй версии.
- function parserss($xmlfeed) {
- $parsed = array();
- //Заголовок
- $parsed["title"] = (string) $xmlfeed->channel->title;
- //Подзаголовок
- $parsed["desc"] = (string) $xmlfeed->channel->description;
- //Тип ленты, хз зачем, пусть будет
- $parsed["type"] = "rss1";
- //Ссылки
- $parsed["links"] = array( "link" => (string) $xmlfeed->channel->link );
- //Сами новости
- if (!empty($xmlfeed->channel->item)) {
- foreach ($xmlfeed->channel->item as $entry) {
- $parsed["entries"][] = array(
- //Заголовок
- "title" => (string) $entry->title,
- //Линки
- "links" => array( "link" => (string) $entry->link ),
- //Содержимое
- "content" => $entry->description
- );
- };
- };
- return $parsed;
- };
- //Ну и RDF на последок
- function parserdf($xmlfeed) {
- $parsed = array();
- //Заголовок
- $parsed["title"] = (string) $xmlfeed->channel->title;
- //Подзаголовок
- $parsed["desc"] = (string) $xmlfeed->channel->description;
- //Тип ленты, хз зачем, пусть будет
- $parsed["type"] = "rdf";
- //Ссылки
- $parsed["links"] = array( "link" => (string) $xmlfeed->channel->link );
- //Сами новости
- if (!empty($xmlfeed->item)) {
- foreach ($xmlfeed->item as $entry) {
- $parsed["entries"][] = array(
- //Заголовок
- "title" => (string) $entry->title,
- //Линки
- "links" => array( "link" => (string) $entry->link ),
- //Содержимое
- "content" => $entry->description
- );
- };
- };
- return $parsed;
- };
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement