feedparser.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. class FeedParser {
  3. private $doc;
  4. private $error;
  5. private $items;
  6. private $link;
  7. private $title;
  8. private $type;
  9. private $xpath;
  10. const FEED_RDF = 0;
  11. const FEED_RSS = 1;
  12. const FEED_ATOM = 2;
  13. function __construct($data) {
  14. libxml_use_internal_errors(true);
  15. libxml_clear_errors();
  16. $this->doc = new DOMDocument();
  17. $this->doc->loadXML($data);
  18. $error = libxml_get_last_error();
  19. if ($error && $error->code == 9) {
  20. libxml_clear_errors();
  21. // we might want to try guessing input encoding here too
  22. $data = iconv("UTF-8", "UTF-8//IGNORE", $data);
  23. $this->doc = new DOMDocument();
  24. $this->doc->loadXML($data);
  25. $error = libxml_get_last_error();
  26. }
  27. $this->error = $this->format_error($error);
  28. libxml_clear_errors();
  29. $this->items = array();
  30. }
  31. function init() {
  32. $root = $this->doc->firstChild;
  33. $xpath = new DOMXPath($this->doc);
  34. $xpath->registerNamespace('atom', 'http://www.w3.org/2005/Atom');
  35. $xpath->registerNamespace('atom03', 'http://purl.org/atom/ns#');
  36. $xpath->registerNamespace('media', 'http://search.yahoo.com/mrss/');
  37. $xpath->registerNamespace('rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#');
  38. $xpath->registerNamespace('slash', 'http://purl.org/rss/1.0/modules/slash/');
  39. $xpath->registerNamespace('dc', 'http://purl.org/dc/elements/1.1/');
  40. $xpath->registerNamespace('content', 'http://purl.org/rss/1.0/modules/content/');
  41. $this->xpath = $xpath;
  42. $root = $xpath->query("(//atom03:feed|//atom:feed|//channel|//rdf:rdf|//rdf:RDF)");
  43. if ($root) {
  44. $root = $root->item(0);
  45. if ($root) {
  46. switch (mb_strtolower($root->tagName)) {
  47. case "rdf:rdf":
  48. $this->type = $this::FEED_RDF;
  49. break;
  50. case "channel":
  51. $this->type = $this::FEED_RSS;
  52. break;
  53. case "feed":
  54. $this->type = $this::FEED_ATOM;
  55. break;
  56. default:
  57. if( !isset($this->error) ){
  58. $this->error = "Unknown/unsupported feed type";
  59. }
  60. return;
  61. }
  62. }
  63. switch ($this->type) {
  64. case $this::FEED_ATOM:
  65. $title = $xpath->query("//atom:feed/atom:title")->item(0);
  66. if (!$title)
  67. $title = $xpath->query("//atom03:feed/atom03:title")->item(0);
  68. if ($title) {
  69. $this->title = $title->nodeValue;
  70. }
  71. $link = $xpath->query("//atom:feed/atom:link[not(@rel)]")->item(0);
  72. if (!$link)
  73. $link = $xpath->query("//atom03:feed/atom03:link[not(@rel)]")->item(0);
  74. if ($link && $link->hasAttributes()) {
  75. $this->link = $link->getAttribute("href");
  76. }
  77. $articles = $xpath->query("//atom:entry");
  78. if (!$articles || $articles->length == 0)
  79. $articles = $xpath->query("//atom03:entry");
  80. foreach ($articles as $article) {
  81. array_push($this->items, new FeedItem_Atom($article, $this->doc, $this->xpath));
  82. }
  83. break;
  84. case $this::FEED_RSS:
  85. $title = $xpath->query("//channel/title")->item(0);
  86. if ($title) {
  87. $this->title = $title->nodeValue;
  88. }
  89. $link = $xpath->query("//channel/link")->item(0);
  90. if ($link) {
  91. if ($link->getAttribute("href"))
  92. $this->link = $link->getAttribute("href");
  93. else if ($link->nodeValue)
  94. $this->link = $link->nodeValue;
  95. }
  96. $articles = $xpath->query("//channel/item");
  97. foreach ($articles as $article) {
  98. array_push($this->items, new FeedItem_RSS($article, $this->doc, $this->xpath));
  99. }
  100. break;
  101. case $this::FEED_RDF:
  102. $xpath->registerNamespace('rssfake', 'http://purl.org/rss/1.0/');
  103. $title = $xpath->query("//rssfake:channel/rssfake:title")->item(0);
  104. if ($title) {
  105. $this->title = $title->nodeValue;
  106. }
  107. $link = $xpath->query("//rssfake:channel/rssfake:link")->item(0);
  108. if ($link) {
  109. $this->link = $link->nodeValue;
  110. }
  111. $articles = $xpath->query("//rssfake:item");
  112. foreach ($articles as $article) {
  113. array_push($this->items, new FeedItem_RSS($article, $this->doc, $this->xpath));
  114. }
  115. break;
  116. }
  117. } else {
  118. if( !isset($this->error) ){
  119. $this->error = "Unknown/unsupported feed type";
  120. }
  121. return;
  122. }
  123. }
  124. function format_error($error) {
  125. if ($error) {
  126. return sprintf("LibXML error %s at line %d (column %d): %s",
  127. $error->code, $error->line, $error->column,
  128. $error->message);
  129. } else {
  130. return "";
  131. }
  132. }
  133. function error() {
  134. return $this->error;
  135. }
  136. function get_link() {
  137. return $this->link;
  138. }
  139. function get_title() {
  140. return $this->title;
  141. }
  142. function get_items() {
  143. return $this->items;
  144. }
  145. function get_links($rel) {
  146. $rv = array();
  147. switch ($this->type) {
  148. case $this::FEED_ATOM:
  149. $links = $this->xpath->query("//atom:feed/atom:link");
  150. foreach ($links as $link) {
  151. if (!$rel || $link->hasAttribute('rel') && $link->getAttribute('rel') == $rel) {
  152. array_push($rv, $link->getAttribute('href'));
  153. }
  154. }
  155. break;
  156. case $this::FEED_RSS:
  157. $links = $this->xpath->query("//atom:link");
  158. foreach ($links as $link) {
  159. if (!$rel || $link->hasAttribute('rel') && $link->getAttribute('rel') == $rel) {
  160. array_push($rv, $link->getAttribute('href'));
  161. }
  162. }
  163. break;
  164. }
  165. return $rv;
  166. }
  167. } ?>