PHP : Smallest RSS feed script
hi, normally when I need to fetch an RSS feed in private project I used some old script based on the php function fopen. But with my current Host (Dreamhost) fopen is dissables for various security reason, they however adviced to use the cURL library, I wasn’t familiar with it but after some reading and testing I’ve build an super small feedreader, well see for yourself . (Oh and you need to have the XML parser module installed on your server).
if (isset($_GET["url"]) and $_GET["url"] != "" ) { $url = $_GET["url"];
$t=0;
$buff="";
if(isset($_GET["name"]) && $_GET["name"] != "" ? $name = $_GET["name"] : $name = "Feed" );
$curl = curl_init();
curl_setopt ($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$xml = simplexml_load_string(curl_exec ($curl));
foreach ($xml->channel->item as $item){
if ($t < 5 ){
$buff.= "<li id=\"lastfm_li\"><a href=\"$item->link\">$item->title</a></li>\n";
$t++;
}
}
curl_close ($curl);
$content.="<div id=\"feed_div\"><p id=\"boxheader\">".$name."</p>\n";
$content.="<ul id=\"feed_ul\">\n";
$content.= $buff;
$content.="</ul>\n";
$content.="</div>\n";
echo $content;
}
Usage.
Save the file on the server call it like this
http://someserver.com/yourfile.php?url=http://urltofeed.com/feed
optional is the name for the feed.
have comments leave them here, and I’ll try to respond as soon as possible!!
Tags: feed, feedreader, php, reader, tutorial
























March 13th, 2008 at 6:16 pm
Nice, not bad at all! Although, should note, SimpleXML is PHP5 only. Also, in your post, it converted && to &&, so before anyone tries to run the script, you need to manually edit that.
March 13th, 2008 at 6:17 pm
In the previous post, that should have been:
“it converted && to it’s html entity”
March 13th, 2008 at 6:22 pm
oh thanks for that notification Eric! I’ll change it right away, and yes you are right it is PHP5 only, but I think only cavemen are still running php4!
November 18th, 2009 at 8:22 pm
The toughest thing about success is that you’ve got to keep on being a success. Talent is only a starting point in this business. You’ve got to keep on working that talent. Someday I’ll reach for it and it won’t be there.