How to Read RSS Feeds with PHP (using SimpleXML)

Check out my other posts and tutorials on Web Development!

I recently rebuilt my personal website and rather than create a personal blog (let’s be honest, I barely find the time to post here!) I decided to pull a few blog and portfolio posts from our business website. To accomplish this, I wrote a couple PHP functions to parse the necessary RSS feeds using SimpleXML.

Create the Blog Post Function

I recommend placing your PHP functions in their own file to keep things organized (if you are using WordPress, place in functions.php).


<?php 
function getPosts($feed_url) {

    $content = file_get_contents($feed_url); // get XML string
    $x = new SimpleXmlElement($content); // load XML string into object

    echo '<ul class="blog-posts clearfix">';
    $i=1; // start count at 1

    // loop through posts
    foreach($x->channel->item as $entry) {
        
        // format blog post output
        echo '<li class="blog-post">';
        echo '<a href="'.$entry->link.'" title="'.$entry->title.'" target="_blank">' . $entry->title . '</a>'; // output link & title
        echo $entry->description; // return post content
        echo '</li>';

        $i++; // increment counter

        if($i >= 3) // if counter more than 2 - quit
        break;        
    }
    echo "</ul>";
} ?>

Display Posts

We can then call our function with our feed url to display the posts from our RSS feed.


<?php getPosts("http://yoururl.com/feed/"); ?>

Create the Portfolio Post Function

For the Portfolio Posts, I wanted to pull the featured images for each post as well. To do this I used the Add featured image to RSS feed Plugin to, you guessed it, add featured images to our RSS feeds.


<?php 
function getPortfolio($feed_url) {

    $content = file_get_contents($feed_url); // get XML string
    $x = new SimpleXmlElement($content); // load XML string into object

    echo '<ul class="portfolio-items clearfix">';
    $i=1; // start count at 1

    // loop through posts
    foreach($x->channel->item as $entry) {

        $content = $entry->description; // get post content
        preg_match('/(<img[^>]+>)/i', $content, $matches); // extract featured image from post description and place in $matches array

        // format portfolio item output
        echo '<li class="portfolio-item">';
        echo '<div><a href="'.$entry->link.'" title="'.$entry->title.'" target="_blank">' . $matches[0] . '</a></div>'; // output first image in $matches array, output link & title
        echo '</li>';

        $i++; // increment counter

        if($i >= 7) // if counter more than 6 - quit
        break;        
    }
    echo "</ul>";
} ?>

Display Portfolio Posts

Once again we call our function with the RSS Feed url for our portfolio custom post type.


<?php getPortfolio("http://yoururl.com/feed/?post_type=portfolio"); ?>

That’s it. Hope this helps!

2 Responses to How to Read RSS Feeds with PHP (using SimpleXML)

  1. Rachmat Abdul Rokhim says:

    You are amazing !
    Im trying to make an array from rss feed (title , first image , desc , etc) and i got some disgusting bug, its make image inside description disapear because html entity was encoded, i have no idea, but your post is so helpfull , THANKS !

    Reply

Leave a Reply

Contact Us

Subscribe

Subscribe to our blog to receive latest news and tutorials on Web Design, Development, SEO, and more!