User Tools

Site Tools


Working with RSS

Let's take a look at the Fediverse feed for the hashtag we're using for the coding meetup:

This is formatted for a web browser. If we look at the source then we can see a line in the HTML that specifies how to access the data in format that can be used by programs (XML).

<link href='' rel='alternate' type='application/rss+xml'>

This is the RSS feed for that page:

RSS can be thought of as a Rich Site Summary, but it more popularly known as Really Simple Syndication as it's often used to retrieve content from other sites to re-display (syndicate).

If you provide lists of information on a web site it's good practice to also provide it as an RSS feed.

Reading RSS

Take a look at the RSS page in your browser:

It might not be very clear, but by comparing the version in the web browser to this RSS version you should be able to identify the content (it starts with the <item> tag and has a <title>, <description>, <link> etc). Here are the first two items (they content will change in your version based on when you view this page).

<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:webfeeds="">
    <description>These are public toots tagged with #coding. You can interact with them if you have an account anywhere in the fediverse.</description>
      <title>New status by neil</title>
      <guid isPermalink="true"></guid>
      <pubDate>Tue, 23 Jul 2019 08:43:48 +0000</pubDate>
      <description>&lt;p&gt;Coding from Scratch meetup tonight in &lt;a href="" class="mention hashtag" rel="tag"&gt;#&lt;span&gt;TheAvalon&lt;/span&gt;&lt;/a&gt; from 6:30pm!  26 signed up so far (without actually &amp;apos;announcing&amp;apos; the meetup&amp;apos; - so it should be mostly people who have been before).  See ya all there! &lt;a href="" class="mention hashtag" rel="tag"&gt;#&lt;span&gt;coding&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;</description>
      <title>New status by Legomancer</title>
      <guid isPermalink="true"></guid>
      <pubDate>Thu, 18 Jul 2019 22:54:18 +0000</pubDate>
      <description>&lt;p&gt;Everyone has a preferred text editor that connects to AWS in different ways. I don’t know them all but this maybe the PHPStorm docs could be a starting point: &lt;br /&gt;&lt;a href="" rel="nofollow noopener" target="_blank"&gt;&lt;span class="invisible"&gt;https://www.&lt;/span&gt;&lt;span class="ellipsis"&gt;;/span&gt;&lt;span class="invisible"&gt;eate-new-project-add-remote-server.html&lt;/span&gt;&lt;/a&gt; &lt;br /&gt;&lt;a href="" class="mention hashtag" rel="tag"&gt;#&lt;span&gt;coding&lt;/span&gt;&lt;/a&gt; &lt;a href="" class="mention hashtag" rel="tag"&gt;#&lt;span&gt;codingfromscratch&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;</description>

The first two lines indicate the format of the data.

The next line <channel> is the main block of data to come (there can be multiple channels), it has a title, description etc.

Then comes multiple <item> sections. These are the actual posts that make up the data. Take a moment to familarise yourself with the structure of the XML file.

Reading the RSS with PHP

Now we can write some code that reads this information and outputs it on your own page (more advanced uses might be to save it to a database or email you a summary daily etc)

$rss_url = "";
// read the RSS file into the $content variable
$content = file_get_contents($rss_url);
// format the RSS data as XML and save it to $data
$data = new simpleXmlElement($content);
// we only care about the first (and only) channel, so access it directly with $data->channel->item
foreach($data->channel->item as $item_id=>$objItem) {
  echo "<p>".$objItem->description."</p>";
coding/working_with_rss.txt · Last modified: 2023/06/26 03:11 by admin