How to Parse RSS and Atom Feeds With PHP
-
Use
implode()
andsimplexml_load_string()
to Parse RSS Feeds in PHP -
Use
simplexml_load_file()
to Parse RSS Feeds in PHP
RSS (Really Simple Syndication
) was a popular feed that allowed early internet users to access website updates in a concurrent and consistent readable format, just like JSON is to APIs. However, RSS is rarely popular nowadays, and the last stable release for the format was in 2009, 13 years ago.
As with obtaining and reading JSON in PHP or parsing HTML, we can make use of built-in functions from PHP to obtain and parse RSS feeds in PHP.
In this article, we discuss the PHP library, which has two built-in functions that will help us parse RSS in PHP, and a built-in function to help with the whole process.
Use implode()
and simplexml_load_string()
to Parse RSS Feeds in PHP
The SimpleXML
library contains tons of functions that can work with XML to convert them to objects that we can use PHP to manipulate. The first function is simplexml_load_string()
which will help us to parse RSS feeds in PHP.
To understand how to parse the RSS feed, we will use this RSS feed, The Daily by New York Times, and get the array of all the content that’s within the feed.
The file()
function reads the RSS feed and returns an array of all the content, and the implode()
function then joins all elements of the array to a string and returns a string.
This string value is passed to the simplexml_load_string()
, which interprets a string of XML into an object which is then encoded using the json_encode()
function and decoded to an array with the json_decode()
function which we can work with.
<?php
$feed = implode(file('https://feeds.simplecast.com/54nAGcIl'));
$xml = simplexml_load_string($feed);
$json = json_encode($xml);
$array = json_decode($json,TRUE);
var_dump($array);
?>
Output:
Use simplexml_load_file()
to Parse RSS Feeds in PHP
Just like simplexml_load_string()
, the function simplexml_load_file()
interprets an XML.
However, the XML file interprets rather than the XML string in this case. It returns an object as well.
Since both return the same values, in this code, we loop through the code to pick specific values from the title, link, description, and publication date, which can be applied to the previous code snippet.
To do so, we make use of the tag channel
, which is a child object to $xml
, and then the item[$i]
, which is a grandchild to the tag, which allows us to get access to different properties (object) such as title, link, etc.
<?php
$url = "https://feeds.simplecast.com/54nAGcIl";
$xml = simplexml_load_file($url);
for ($i = 0; $i < 2; $i++) {
$title = $xml->channel->item[$i]->title;
$link = $xml->channel->item[$i]->link;
$description = $xml->channel->item[$i]->description;
$pubDate = $xml->channel->item[$i]->pubDate;
echo "\nThe Title: " . $title . ".\n";
echo "\nChannel Link: " . $link . ".\n";
echo "\nChannel Description: " . $description . ".\n";
echo "\nDate of Publication: " . $pubDate . ".\n";
}
?>
Output:
Olorunfemi is a lover of technology and computers. In addition, I write technology and coding content for developers and hobbyists. When not working, I learn to design, among other things.
LinkedIn