DaniWeb IT Discussion Community

Code Snippets (http://www.daniweb.com/code/)
-   php (http://www.daniweb.com/code/php.html)
-   -   Export RSS 1.0 or 2.0 feeds to an array (http://www.daniweb.com/code/snippet778.html)

maddog39 php syntax
Oct 31st, 2007
This class will allow you to take an RSS feed (local or remote) and "export" it to an easily managed/viewed array. Below is an example of it usage:
<?php
include("exportrss.php");

// Parse XML/RSS 2.0 feed
$feed = new ExportRSS("test.xml", "2.0");
$channel = $feed->get_channel_data();
echo "<h3>Channel</h3>
<p>
<b>Title:</b> {$channel['title']}<br/>
<b>Date:</b> {$channel['date']}<br/>
<b>Description:</b> {$channel['description']}<br/>
<b>Editor:</b> {$channel['editor']}<br/>
<b>Webmaster:</b> {$channel['webmaster']}<br/>
<b>Language:</b> {$channel['language']}<br/>
<b>Generator:</b> {$channel['generator']}<br/>
<b>Link:</b> {$channel['link']}
</p>";

echo "<h3>Feed Items</h3>";
foreach ($feed->get_data() as $item)
{
        echo "<p>
        <b>Title</b>: {$item['title']}<br/>
        <b>Date Published</b>: {$item['date']}<br/>
        <b>Description</b>: {$item['description']}<br/>
        <b>Link</b>: {$item['link']}</p>";
}
?>

  1. <?php
  2. /*
  3.  * exportrss.php
  4.  *
  5.  * Copyright 2007 Alec Hussey <alec.hussey@gmail.com>
  6.  *
  7.  * This program is free software; you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License as published by
  9.  * the Free Software Foundation; either version 2 of the License, or
  10.  * (at your option) any later version.
  11.  *
  12.  * This program is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15.  * GNU General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU General Public License
  18.  * along with this program; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  20.  * MA 02110-1301, USA.
  21.  */
  22.  
  23. class ExportRSS
  24. {
  25. protected $data = array();
  26. protected $channel = array();
  27. protected $rawdata = array();
  28.  
  29. public function __construct($data, $version)
  30. {
  31. $feed = @file_get_contents($data);
  32. $this->rawdata = simplexml_load_string($feed);
  33.  
  34. // Parse the XML/RSS file
  35. switch ($version)
  36. {
  37. case "1.0":
  38. {
  39. $this->channel = array(
  40. "title" => $this->rawdata->channel->title,
  41. "link" => $this->rawdata->channel->link,
  42. "description" => $this->rawdata->channel->description,
  43. "image" => $this->rawdata->channel->image,
  44. "items" => $this->rawdata->channel->items,
  45. "textinput" => $this->rowdata->channel->textinput
  46. );
  47.  
  48. foreach ($this->rawdata->item as $item)
  49. {
  50. $row = array(
  51. "title" => $item->title,
  52. "link" => $item->link,
  53. "description" => $item->description
  54. );
  55. array_push($this->data, $row);
  56. }
  57. break;
  58. }
  59. case "2.0":
  60. {
  61. $this->channel = array(
  62. "title" => $this->rawdata->channel->title,
  63. "link" => $this->rawdata->channel->link,
  64. "description" => $this->rawdata->channel->description,
  65. "language" => $this->rawdata->channel->language,
  66. "date" => $this->rawdata->channel->pubDate,
  67. "builddate" => $this->rawdata->channel->lastBuildDate,
  68. "docs" => $this->rawdata->channel->docs,
  69. "generator" => $this->rawdata->channel->generator,
  70. "editor" => $this->rawdata->channel->managingEditor,
  71. "webmaster" => $this->rawdata->channel->webMaster,
  72. );
  73.  
  74. foreach ($this->rawdata->channel->item as $item)
  75. {
  76. $row = array(
  77. "title" => $item->title,
  78. "link" => $item->link,
  79. "description" => $item->description,
  80. "enclosure" => $this->enclosure
  81. "date" => $item->pubDate,
  82. "guid" => $item->guid
  83. );
  84. array_push($this->data, $row);
  85. }
  86. break;
  87. }
  88. default:
  89. {
  90. echo "ExportRSS::WARNING: invalid version was specified";
  91. break;
  92. }
  93. }
  94. }
  95.  
  96. public function get_raw_data()
  97. {
  98. return $this->rawdata;
  99. }
  100.  
  101. public function get_channel_data()
  102. {
  103. return $this->channel;
  104. }
  105.  
  106. public function get_data()
  107. {
  108. return $this->data;
  109. }
  110. }
  111. ?>