How to get RSS from wordpress using PHP

ichii731

Ichi

Posted on June 21, 2021

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

Sample

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

https://0115765.com/samples/rss.php
RSS

Source Code

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

<?php
// Get RSS&Pursing XML
$rss = simplexml_load_file('https://0115765.com/feed');

$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>";
    $cnt++;

    // 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.

<item>
    <title>
        Article Title
    </title>
    <link>
        https://0115765.com/archives/xxxx
    </link>
    <comments>
        https://0115765.com/archives/xxxx#respond
    </comments>
    <creator>
        Creator
    </creator>
    <pubDate>
        Publish Date
    </pubDate>
    <category>
        Category
    </category>
    <guid isPermaLink="false">
        https://0115765.com/?p=xxxx
    </guid>
    <description>
        Article Discription
    </description>
    <commentRss>
        https://0115765.com/archives/xxxx/feed
    </commentRss>
    <comments>
        xxxx
    </comments>
</item>

Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
ichii731
Ichi

Posted on June 21, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related