SyntaxStudy
Sign Up
PHP Parsing RSS Feeds with PHP
PHP Intermediate 5 min read

Parsing RSS Feeds with PHP

RSS Feed Parsing

RSS is XML-based. SimpleXML can parse RSS feeds directly. For robust feed handling across RSS and Atom formats, use a library.

Example
<?php
$feed = simplexml_load_file("https://example.com/feed.rss");

// Handle XML namespaces in RSS 2.0
$feed->registerXPathNamespace("media", "http://search.yahoo.com/mrss/");

foreach ($feed->channel->item as $item) {
    $title   = (string) $item->title;
    $link    = (string) $item->link;
    $pubDate = (string) $item->pubDate;
    $desc    = (string) $item->description;

    // Get media content (using namespace)
    $media = $item->children("media", true)->content;
    $image = (string) $media->attributes()->url;

    echo "<h2>{$title}</h2><p>{$desc}</p>";
}
Pro Tip

Use registerXPathNamespace() before XPath queries on feeds with namespaces like media: or content:.