Export RSS 1.0 or 2.0 feeds to an array
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | < ?php /* exportrss.php * Copyright 2007 Alec Hussey <alec.hussey@gmail.com> * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, * MA 02110-1301, USA. */ class ExportRSS{ protected $data = array(); protected $channel = array(); protected $rawdata = array(); public function __construct($data, $version) { $feed = @file_get_contents($data); $this->rawdata = simplexml_load_string($feed); // Parse the XML/RSS file switch ($version) { case "1.0": { $this->channel = array( "title" => $this->rawdata->channel->title, "link" => $this->rawdata->channel->link, "description" => $this->rawdata->channel->description, "image" => $this->rawdata->channel->image, "items" => $this->rawdata->channel->items, "textinput" => $this->rowdata->channel->textinput ); foreach ($this->rawdata->item as $item) { $row = array( "title" => $item->title, "link" => $item->link, "description" => $item->description ); array_push($this->data, $row); } break; } case "2.0": { $this->channel = array( "title" => $this->rawdata->channel->title, "link" => $this->rawdata->channel->link, "description" => $this->rawdata->channel->description, "language" => $this->rawdata->channel->language, "date" => $this->rawdata->channel->pubDate, "builddate" => $this->rawdata->channel->lastBuildDate, "docs" => $this->rawdata->channel->docs, "generator" => $this->rawdata->channel->generator, "editor" => $this->rawdata->channel->managingEditor, "webmaster" => $this->rawdata->channel->webMaster, ); foreach ($this->rawdata->channel->item as $item) { $row = array( "title" => $item->title, "link" => $item->link, "description" => $item->description, "enclosure" => $this->enclosure, "date" => $item->pubDate, "guid" => $item->guid ); array_push($this->data, $row); } break; } default: { echo "ExportRSS::WARNING: invalid version was specified"; break; } } } public function get_raw_data() { return $this->rawdata; } public function get_channel_data() { return $this->channel; } public function get_data() { return $this->data; } }?> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | < ?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> <strong>Title:</strong> {$channel['title']} <strong>Date:</strong> {$channel['date']} <strong>Description:</strong> {$channel['description']} <strong>Editor:</strong> {$channel['editor']} <strong>Webmaster:</strong> {$channel['webmaster']} <strong>Language:</strong> {$channel['language']} <strong>Generator:</strong> {$channel['generator']} <strong>Link:</strong> {$channel['link']} "; echo " <h3>Feed Items</h3> "; foreach ($feed->get_data() as $item) { echo " <strong>Title</strong>: {$item['title']} <strong>Date Published</strong>: {$item['date']} <strong>Description</strong>: {$item['description']} <strong>Link</strong>: {$item['link']} "; } ?> |
RSS Class – PHP
This is a generation RSS document PHP class
<?php
if (defined(’_CLASS_RSS_PHP’)) return;
define(’_CLASS_RSS_PHP’,1);
/**
* Class name: RSS
* $rss = new RSS(”Blog”,”http://www.easemarry.com/”,”EaseMarry’s Blog”);
* $rss->AddItem(”RSS Class”,”http://www.easemarry.com”,”description”,date());
* $rss->AddItem(…);
* $rss->SaveToFile(”../rss/index.xml”);
*/
class RSS {
var $rss_ver = “2.0″;
var $channel_title = ”;
var $channel_link = ”;
var $channel_description = ”;
var $language = ‘en-us’;
var $copyright = ‘easemarr.com’;
var $webMaster = ‘web@easemarry.com’;
var $pubDate = ”;
var $generator = ‘Easemarry.com’;
var $content = ”;
var $items = array();
function RSS($title, $link, $description) {
date_default_timezone_set(’GMT’);
$this->channel_title = $title;
$this->channel_link = $link;
$this->channel_description = $description;
$this->pubDate = Date(DATE_RFC822);
}
function AddItem($title, $link, $description ,$pubDate) {
$this->items[] = array(’titile’ => $title ,
’link’ => $link,
’description’ => $description,
’pubDate’ => $pubDate);
}
function BuildRSS() {
$s = “<?xml version=\”1.0\” encoding=\”UTF-8\” ?>\n<rss version=\”2.0\”> \n”;
// start channel
$s .= “<channel>\n”;
$s .= “<title>$this->channel_title</title>\n”;
$s .= “<link>$this->channel_link</link>\n”;
$s .= “<description>$this->channel_description</description>\n”;
$s .= “<language>$this->language</language>\n”;
if (!empty($this->copyright)) {
$s .= “<copyright>$this->copyright</copyright>\n”;
}
if (!empty($this->webMaster)) {
$s .= “<webMaster>$this->webMaster</webMaster>\n”;
}
if (!empty($this->pubDate)) {
$s .= “<pubDate>$this->pubDate</pubDate>\n”;
}
if (!empty($this->generator)) {
$s .= “<generator>$this->generator</generator>\n”;
}
// start items
if (count($this->items)>=1){
foreach ($this->items as $key=>$value){
$s .= “<item>\n”;
$s .= “<title>”.$value['titile'].”</title>\n”;
$s .= “<link>”.$value['link'].”</link>\n”;
$s .= “<description>”.$value['description'].”</description>\n”;
$s .= “<pubDate>”.$value['pubDate'].”</pubDate>\n”;
$s .= “<guid isPermaLink=\”true\”>”.$value['link'].”</guid>”;
$s .= “</item>\n”;
}
}
// close channel
$s .= “</channel>\n</rss>”;
$this->content = $s;
}
function Show() {
if (empty($this->content)) $this->BuildRSS();
header(’Content-type:text/xml’);
echo($this->content);
}
function SaveToFile($fname) {
if (empty($this->content)) $this->BuildRSS();
$handle = fopen($fname, ‘wb’);
if ($handle === false) {
return false;
}else{
fwrite($handle, $this->content);
fclose($handle);
return true;
}
}
}
?>

