DEV Community

Ichi
Ichi

Posted on

5 2

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

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

The best way to debug slow web pages cover image

The best way to debug slow web pages

Tools like Page Speed Insights and Google Lighthouse are great for providing advice for front end performance issues. But what these tools can’t do, is evaluate performance across your entire stack of distributed services and applications.

Watch video

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay