Friday, July 17

RSS PHP: Tutorial #1

In this first RSS PHP Tutorial, we'll look at how PHP uses XML to parse the RSS file.

Here's an example of parsing a RSS file using the function below.

$rss_tags = array(
‘title’,
‘link’,
‘guid’,
‘comments’,
‘description’,
‘pubDate’,
‘category’,
);
$rss_item_tag = ‘item’;
$rss_url = ‘http://rssphp.blogspot.com/feeds/posts/default’;

$rssfeed = rss_to_array($rss_item_tag,$rss_tags,$rss_url);

print_r($rssfeed);

function rss_to_array($tag, $array, $url) {
$doc = new DOMdocument();
$doc->load($url);
$rss_array = array();
$items = array();
foreach($doc->getElementsByTagName($tag) AS $node) {
foreach($array AS $key => $value) {
$items[$value] = $node->getElementsByTagName($value)->item(0)->nodeValue;
}
array_push($rss_array, $items);
}
return $rss_array;
}
?>


And there you have your own RSS parser. All we did was put the feed items into an array, now you do the rest!

1 comment:

 

Template by James William at 2600 Degrees