User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 331,473 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,072 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Oct 31st, 2007
Views: 1,293
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:
  1. <?php
  2. include("exportrss.php");
  3.  
  4. // Parse XML/RSS 2.0 feed
  5. $feed = new ExportRSS("test.xml", "2.0");
  6. $channel = $feed->get_channel_data();
  7. echo "<h3>Channel</h3>
  8. <p>
  9. <b>Title:</b> {$channel['title']}<br/>
  10. <b>Date:</b> {$channel['date']}<br/>
  11. <b>Description:</b> {$channel['description']}<br/>
  12. <b>Editor:</b> {$channel['editor']}<br/>
  13. <b>Webmaster:</b> {$channel['webmaster']}<br/>
  14. <b>Language:</b> {$channel['language']}<br/>
  15. <b>Generator:</b> {$channel['generator']}<br/>
  16. <b>Link:</b> {$channel['link']}
  17. </p>";
  18.  
  19. echo "<h3>Feed Items</h3>";
  20. foreach ($feed->get_data() as $item)
  21. {
  22. echo "<p>
  23. <b>Title</b>: {$item['title']}<br/>
  24. <b>Date Published</b>: {$item['date']}<br/>
  25. <b>Description</b>: {$item['description']}<br/>
  26. <b>Link</b>: {$item['link']}</p>";
  27. }
  28. ?>
php Syntax | 5 stars
  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. ?>
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 8:44 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC