How to read an RSS Feed using PHP

There are many ways 2 read XML Feed using PHP.

Some basic ways with which this can be done are given below along with code:

Basic Way 1

[php]
<?php
function getFeed($feed_url) {
$content = file_get_contents($feed_url);
$x = new SimpleXmlElement($content);

echo "<ul>";

foreach($x->channel->item as $entry) {
echo "<li><a href=’$entry->link’ title=’$entry->title’>" . $entry->title . "</a></li>";
}
echo "</ul>";
}
getFeed(‘http://somefeedurl/’)
?>
[/php]

In the above method, create a function and call that function.

basic Way 2

[php]
<?php
include(‘rssclass.php’);
$feedlist = new rss(‘http://feeds2.feedburner.com/9lesson’);
<a href="http://www.php.net/echo">echo</a> $feedlist->display(9,"9lessons");

$feedlist = new rss(‘http://feeds.feedburner.com/nettuts’);
<a href="http://www.php.net/echo">echo</a> $feedlist->display(9,"Nettuts");

$feedlist = new rss(‘http://feeds.labnol.org/labnol’);
<a href="http://www.php.net/echo">echo</a> $feedlist->display(9,"Labnol");
?>

[/php]
[php]
<?php
class rss {
var $feed;

function rss($feed)
{ $this->feed = $feed; }

function parse()
{
$rss = simplexml_load_file($this->feed);

$rss_split = <a href="http://www.php.net/array">array</a>();
 foreach ($rss->channel->item as $item) {
$title = (string) $item->title; // Title
$link = (string) $item->link; // Url Link
$description = (string) $item->description; //Description
$rss_split[] = ‘<div>
<a href="’.$link.’" target="_blank" title="" >
‘.$title.’
</a>
<hr>
</div>
‘;
}
return $rss_split;
}
function display($numrows,$head)
{
$rss_split = $this->parse();

$i = 0;
$rss_data = ‘<div>
<div>
‘.$head.’
</div>
<div>’;
while ( $i < $numrows )
{
$rss_data .= $rss_split[$i];
$i++;
}
$trim = <a href="http://www.php.net/str_replace">str_replace</a>(”, ”,$this->feed);
$user = <a href="http://www.php.net/str_replace">str_replace</a>(‘&lang=en-us&format=rss_200′,”,$trim);
$rss_data.='</div></div>’;
return $rss_data;
}
}
?>
[/php]
[php]
.vas{
float:left;
width:270px;
padding:10px;
}
.title-head {
font-size:18px;
font-weight:bold;
text-align:left;
background-color:#006699;
color:#FFFFFF;
padding:5px;}
.feeds-links {
text-align:left;
padding:5px;
border:1px solid #dedede;
}
[/php]

Basic Way 3

using SimpleXML

[php]
$doc = new SimpleXmlElement($data, LIBXML_NOCDATA);
print_r($doc);
if(isset($doc->channel))
{
parseRSS($doc);
}
if(isset($doc->entry))
{
parseAtom($doc);
}

function parseRSS($xml)
{
echo "<strong>".$xml->channel->title."</strong>";
$cnt = count($xml->channel->item);
for($i=0; $i<$cnt; $i++)
{
$url = $xml->channel->item[$i]->link;
$title = $xml->channel->item[$i]->title;
$desc = $xml->channel->item[$i]->description;

echo ‘<a href="’.$url.’">’.$title.'</a>’.$desc.”;
}
}
function parseAtom($xml)
{
echo "<strong>".$xml->author->name."</strong>";
$cnt = count($xml->entry);
for($i=0; $i<$cnt; $i++)
{
$urlAtt = $xml->entry->link[$i]->attributes();
$url = $urlAtt[‘href’];
$title = $xml->entry->title;
$desc = strip_tags($xml->entry->content);

echo ‘<a href="’.$url.’">’.$title.'</a>’.$desc.”;
}
}
[/php]

Basic Way 4

using Curl

[php]
$ch = curl_init("http://localhost/curl/rss.xml")
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
[/php]

Leave a Comment