20111209 retrieve and display rss with php - plembo/onemoretech GitHub Wiki

title: Retrieve and display rss with php link: https://onemoretech.wordpress.com/2011/12/09/retrieve-and-display-rss-with-php/ author: lembobro description: post_id: 1808 created: 2011/12/09 16:24:16 created_gmt: 2011/12/09 20:24:16 comment_status: closed post_name: retrieve-and-display-rss-with-php status: publish post_type: post

Retrieve and display rss with php

Needed a simple script to test out RSS handling in php. This one worked (which is more than I can say for many I tried). From bavotasan.com, a really simple script to retrieve and display rss feeds using php. This requires the DOM extension included with php since version 5.2 be loaded. You also need to enable allow_url_fopen in /etc/php.ini (or wherever your php.ini is.

allow_url_fopen = On

I named the script "getrss.php". [code language="php"]getElementsByTagName('item') as $node) { $item = array ( 'title' => $node->getElementsByTagName('title')->item(0)->nodeValue, 'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue, 'link' => $node->getElementsByTagName('link')->item(0)->nodeValue, 'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue, ); array_push($feed, $item); } $limit = 5; for($x=0;$x<$limit;$x++) { $title = str_replace(' & ', ' & ', $feed[$x]['title']); $link = $feed[$x]['link']; $description = $feed[$x]['desc']; $date = date('l F d, Y', strtotime($feed[$x]['date'])); echo '

'.$title.'
'; echo 'Posted on '.$date.'

'; echo '

'.$description.'

'; } ?> [/code] The rss feed url is defined in $rss->load, and the number of posts to retrive in "$limit". There's also a very good article on php and rss over at IBM Developerworks, PHP and RSS: Getting it together.

Copyright 2004-2019 Phil Lembo

⚠️ **GitHub.com Fallback** ⚠️