Advertisement
imcrazytwkr

RSS/RSS2/RDF/ATOM Parser with json output

Jul 16th, 2014
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.62 KB | None | 0 0
  1. <?php
  2.     if(!isset($_GET["feed"])) {
  3.         http_response_code(400);
  4.         echo "<h1>400 Bad Request</h1>";
  5.         echo "The request cannot be fulfilled due to lack of parameters.";
  6.         exit(400);
  7.     }
  8.     $jsdt = json_decode($_GET["feed"], true);
  9.     if(!isset($jsdt["type"]) | !isset($jsdt["url"])){
  10.         http_response_code(400);
  11.         echo "<h1>400 Bad Request</h1>";
  12.         echo "The request cannot be fulfilled due to bad syntax.";
  13.         exit(400);
  14.     }
  15.     $feed = file_get_contents($jsdt["url"]);
  16.     switch ($jsdt["type"]) {
  17.         case "atom":
  18.             echo json_encode(parseatom(simplexml_load_string($feed)));
  19.             break;
  20.         case "rss2":
  21.             echo json_encode(parserss2(simplexml_load_string($feed)));
  22.             break;
  23.         case "rss":
  24.             echo json_encode(parserss(simplexml_load_string($feed)));
  25.             break;
  26.         case "rdf":
  27.             echo json_encode(parserdf(simplexml_load_string($feed)));
  28.             break;
  29.         default:
  30.             http_response_code(406);
  31.             break;
  32.     }
  33.     //Парсинг ака ДИЧАЙШИЙ ГОВНОКОД
  34.     function parseatom($xmlfeed)
  35.     {
  36.             $parsed = array();
  37.             //Заголовок
  38.             $parsed["title"] = (string) $xmlfeed->title;
  39.             //Подзаголовок
  40.             $parsed["desc"] = (string) $xmlfeed->subtitle;
  41.             //Тип ленты, хз зачем, пусть будет
  42.             $parsed["type"] = "atom";
  43.             //Ссылки
  44.             foreach ($xmlfeed->link as $link) {     $parsed["links"][(string) $link["rel"]] = (string) $link["href"]; };
  45.             //Сами новости
  46.             if (!empty($xmlfeed->entry)) {
  47.                     foreach ($xmlfeed->entry as $entry) {
  48.                             //Категории
  49.                             $cats = array();
  50.                             foreach ($entry->category as $category) { $cats[] = (string) $category["term"]; };
  51.                             //Линки
  52.                             $entrylink = array();
  53.                             foreach ($entry->link as $link) { $entrylink[(string) $link["rel"]] = (string) $link["href"]; };
  54.                             //Проверка на краткое описание
  55.                             if (isset($entry->summary)) {$desc = (string) $entry->summary;}
  56.                             else $desc = "";
  57.                             //Ну, пошло-поехало
  58.                             $parsed["entries"][] = array(
  59.                                     //Заголовок
  60.                                     "title" => (string) $entry->title,
  61.                                     //Краткое описание
  62.                                     "desc" => $desc,
  63.                                     //Категории
  64.                                     "categories" => $cats,
  65.                                     //Дата-время
  66.                                     "date" => date('Y-m-d h:i:s A', strtotime((string) $entry->published)),
  67.                                     //Линки
  68.                                     "links" => $entrylink,
  69.                                     //Автор
  70.                                     "author" => array(
  71.                                             "name" => (string) $entry->author->name,
  72.                                             "url" => (string) $entry->author->uri
  73.                                             ),
  74.                                     //Содержимое
  75.                                     "content" => (string) $entry->content
  76.                                     );
  77.                     };
  78.             };
  79.             return $parsed;
  80.     };
  81.     //ЭТУ ЕБАНИНУ Я ЕЩЕ НА РАБОТОСПОСОБНОСТЬ НЕ ПРОВЕРЯЛ
  82.     function parserss2($xmlfeed) {
  83.             $parsed = array();
  84.             //Заголовок
  85.             $parsed["title"] = (string) $xmlfeed->channel->title;
  86.             //Подзаголовок
  87.             $parsed["desc"] = (string) $xmlfeed->channel->description;
  88.             //Тип ленты, хз зачем, пусть будет
  89.             $parsed["type"] = "rss2";
  90.             //Ссылки
  91.             $parsed["links"] = array( "link" => (string) $xmlfeed->channel->link );
  92.             //ЭТО СДЕЛАНО СПЕЦИАЛЬНО! Иначе зацикливается к ебеням на первой ячейке!!!!!!!
  93.             $namespaces = $xmlfeed->getNamespaces(true);
  94.             //Даже не спрашивайте, что это, ок?
  95.             foreach ($namespaces as $namespace => $namespaceValue) { $xmlfeed->registerXPathNamespace($namespace, $namespaceValue); };
  96.             //Сами новости
  97.             if (!empty($xmlfeed->channel->item)) {
  98.                     foreach ($xmlfeed->channel->item as $entry) {
  99.                             //Категории
  100.                             $cats = array();
  101.                             foreach ($entry->children() as $child) { if ($child->getName() == "category") { $cats[] = (string) $child; } };
  102.                             //Автор
  103.                             if (!empty($namespaces["dc"]) && $creator = $item->xpath("dc:creator")) { $author = (string) $creator[0]; }
  104.                             else { $author = ""; };
  105.                             //Содержимое
  106.                             if (!empty($namespaces["encoded"]) && $encoded = $item->xpath("content:encoded")) { $content = (string) $encoded[0]; }
  107.                             else {$content = "";};
  108.                             //Проверка на краткое описание
  109.                             if (isset($entry->description)) {$desc = (string) $entry->description;}
  110.                             else $desc = "";
  111.                             //Ну, пошло-поехало
  112.                             $parsed["entries"][] = array(
  113.                                     //Заголовок
  114.                                     "title" => (string) $entry->title,
  115.                                     //Краткое описание
  116.                                     "desc" => $desc,
  117.                                     //Категории
  118.                                     "categories" => $cats,
  119.                                     //Дата-время
  120.                                     "date" => date('Y-m-d h:i:s A', strtotime((string) $entry->pubDate)),
  121.                                     //Линки
  122.                                     "links" => array( "link" => (string) $entry->link ),
  123.                                     //Автор
  124.                                     "author" => array( "name" => $author ),
  125.                                     //Содержимое
  126.                                     "content" => (string) $entry->content
  127.                                     );
  128.                     };
  129.             };
  130.             return $parsed;
  131.     };
  132.     //А теперь старый добрый RSS первой версии без неведомой ебанины, в которую его превратили во второй версии.
  133.     function parserss($xmlfeed) {
  134.             $parsed = array();
  135.             //Заголовок
  136.             $parsed["title"] = (string) $xmlfeed->channel->title;
  137.             //Подзаголовок
  138.             $parsed["desc"] = (string) $xmlfeed->channel->description;
  139.             //Тип ленты, хз зачем, пусть будет
  140.             $parsed["type"] = "rss1";
  141.             //Ссылки
  142.             $parsed["links"] = array( "link" => (string) $xmlfeed->channel->link );
  143.             //Сами новости
  144.             if (!empty($xmlfeed->channel->item)) {
  145.                     foreach ($xmlfeed->channel->item as $entry) {
  146.                             $parsed["entries"][] = array(
  147.                                     //Заголовок
  148.                                     "title" => (string) $entry->title,
  149.                                     //Линки
  150.                                     "links" => array( "link" => (string) $entry->link ),
  151.                                     //Содержимое
  152.                                     "content" => $entry->description
  153.                                     );
  154.                     };
  155.             };
  156.             return $parsed;
  157.     };
  158.     //Ну и RDF на последок
  159.     function parserdf($xmlfeed) {
  160.             $parsed = array();
  161.             //Заголовок
  162.             $parsed["title"] = (string) $xmlfeed->channel->title;
  163.             //Подзаголовок
  164.             $parsed["desc"] = (string) $xmlfeed->channel->description;
  165.             //Тип ленты, хз зачем, пусть будет
  166.             $parsed["type"] = "rdf";
  167.             //Ссылки
  168.             $parsed["links"] = array( "link" => (string) $xmlfeed->channel->link );
  169.             //Сами новости
  170.             if (!empty($xmlfeed->item)) {
  171.                     foreach ($xmlfeed->item as $entry) {
  172.                             $parsed["entries"][] = array(
  173.                                     //Заголовок
  174.                                     "title" => (string) $entry->title,
  175.                                     //Линки
  176.                                     "links" => array( "link" => (string) $entry->link ),
  177.                                     //Содержимое
  178.                                     "content" => $entry->description
  179.                                     );
  180.                     };
  181.             };
  182.             return $parsed;
  183.     };
  184. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement