DEV Community


Posted on

How to get RSS from wordpress using PHP

Good evening. Today, I'll introduce how to get arbitrary information from RSS feed and display it in HTML.
Recently, I'm developing only with Laravel, so I didn't write PHP in the vanilla environment, so make a note so that you don't forget it.

Sample & Code


This time, I got the latest 5 articles from the RSS feed of this blog.(It’s displayed in Japanese)

Source Code

It's a very simple mechanism that just reads and parses the XML.

// Get RSS&Pursing XML
$rss = simplexml_load_file('');

$cnt = 0;
foreach($rss->channel->item as $val){
    // Make HTML
    echo "<a href='" . $val->link . "'>" . $val->title . "</a><br>";
    echo date("Y/m/d", strtotime($val->pubDate)) . "<br>";

    // End with the number of items you want to display.
    if ($cnt==5) {
        echo "View Older Article";
        break; // Be sure to end with break.
Enter fullscreen mode Exit fullscreen mode

Please also check the blog and Twitter if you like :D
Twitter @tomox0115
My Profile&Portfolio

XML Composition

The XML structure of each article in the RSS feed looks like this.

        Article Title
        Publish Date
    <guid isPermaLink="false">
        Article Discription

Enter fullscreen mode Exit fullscreen mode

Top comments (0)