Cesar D. Rodas, web development. Technology news. PHP, MySQL, Apache, C, Bash, ASM

PHP: The easiest way to write and Read RSS

RSS is wild use by blogs, news sites and many others sites by entries syndication. I wrote a class (of course public domain) called gFeed that create a Stream Wrapper for easy manipulation such us creation, append and read. I’ve focus in try to give a easy interface. I hope you like it. Below are a few of examples.

How to read an remote RSS :

<?
include(“gFeed.php”);
$f = fopen(“rss://http://www.cesarodas.com/feed/”,“r”);
/* Rewinding && reading the first line, which is the RSS information. */

rewind($f);
fread($f,1);

print “<h1>${title}</h1>”;
print
“<b>Link</b>: ${link}<br/>\r\n”;
print
“<b>Description</b>: ${description}<br/>\r\n”;
print
“<b>Generator</b>: ${generator}<br/>\r\n”;
print
“<b>Lenguage</b>: ${language}<br/>\r\n”;
print
“<b>Encoding</b>: ${encoding}<br/>\r\n”;

print “<br/>\r\n”;

print “<hr>\r\n”;

while ( fread($f, 1) ) {
/*

*    After the first read (information of blog), comes

*    the RSS entries

*/

print “<b>Title</b>: ${title}<br/>\r\n”;

print “<b>Link</b>: ${link}<br/>\r\n”;

print “<b>Author</b>: ${author}<br/>\r\n”;

print “<b>Date</b>: ${date}<br/>\r\n”;

print “<b>Description</b>: ${description}<br/>\r\n”;

print “<hr>\r\n”;

}

fclose($f);
?>

For open a local file just change the URI rss://file:// or rss://c:/ or rss://../..

How to write a RSS :

<?
include(“gFeed.php”);
/*
* Opening a local RSS for write a new one
*/
$f = fopen(“rss://newRSS.xml”,“w”);
/*
* Do never forget that in the 0 position the channel information
* is saved, but after that (from 1 to N) items are saved.
*/
$title=“gFeed RSS test”;

$link=“http://cesars.users.phpclasses.org/gfeed”;

$description=“RSS IO stream wreaper class”;

$date = date(“Y-m-d H:i:s”);
$language = “en”;
fwrite($f,1);
/* Add a new Entry */
$title = “Test Entry N 1″;
$link = “http://cesars.users.phpclasses.org/gfeed?test”;
$author = “Cesar D. Rodas”;
$date = ‘2007-07-24 14:17:00′;
$description = “Test description”;
fwrite($f,1);
$title = “Test Entry N 2″;
$link = “http://cesars.users.phpclasses.org/gfeed?test”;
$author = “Cesar D. Rodas”;
$date = ‘2007-07-24 14:17:00′;
$description = “Test description”;
fwrite($f,1);
fclose($f);

?>

Donwload gFeed for free

How to append the RSS from top:

<?
include(“gFeed.php”);

$f = fopen(“rss://newRSS.xml”,“a”);
$title = “Test Entry N 3″;
$link = “http://cesars.users.phpclasses.org/gfeed?test”;
$author = “Cesar D. Rodas”;
$date = ‘2007-07-24 14:17:00′;
$description = “Test description”;
fwrite($f,1);
fclose($f);

?>

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages. Digg del.icio.us StumbleUpon Technorati BlinkList Furl NewsVine Reddit

2 Responses to “PHP: The easiest way to write and Read RSS”

  1. Lamonte @phpdiscovery.com Says:

    To me it depends on the type of feed your trying to read, does this library support multidemtional arrays, as in child elements that feeds use sort of like wordpress or blogger?

    I’ll try it out though, thanks for the post. +dzone

  2. admin Says:

    The class only read and write RSS2.

    I don’t know what you mean with “multidemtional arrays”. I’ve try with workpress, and blogger, and works ok.

    Thank you for write, and if have any doubt feel free to ask free support on the php-classes site, or post here.

    best regards

Leave a Reply


Fatal error: Call to undefined function display_cryptographp() in /home/.schmuckie/saddor/cesarodas.com/wp-content/themes/first-spring-10/comments.php on line 94