Post view page

Post #17

Reading a feed
2012-01-05 06:28:11

I needed to read a feed from a wordpress blog so I used the following.  I start by declaring the URI and then reading the data using the PHP file content reader.  This is then passed to the simple xml element for processing.  Once I have the XML from the feed it is just a matter for walking the items with a foreach and print the data.  In this case I use a line item.  I also put the title of the blog and description on the top.  The below code was for testing and had to be modifed for the client server enviroment.

<div>
<?php
$uri = "http://www.site.com/blog/?feed=rss2";
$rawFeed = file_get_contents($uri);
$xml = new SimpleXmlElement($rawFeed);
 
echo 'Title: ' . $xml->channel->title;
echo '</br>';
echo 'Description: ' . $xml->channel->description;
echo '</br>';
 
foreach ($xml->channel->item as $item)
{
?>
<li> <a href='<?php echo $item->link; ?>'
     title='<?php echo 'Posted ' . date('j F Y | g:i a', $item->pubDate)?>'> <?php echo $item->title; ?></a> </li>
 
<?php
}
?>
</div>

 

Test right content

first test

Test right content

second test

Test right content

third test

this is a test