User Tools

Site Tools


working_with_rss

This is an old revision of the document!


Working with RSS

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

https://glasgow.social/tags/coding

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='https://glasgow.social/tags/coding.rss' rel='alternate' type='application/rss+xml'>

This is the RSS feed for that page:

https://glasgow.social/tags/coding.rss

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: https://glasgow.social/tags/coding.rss

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="http://webfeeds.org/rss/1.0">
  <channel>
    <title>#coding</title>
    <description>These are public toots tagged with #coding. You can interact with them if you have an account anywhere in the fediverse.</description>
    <link>https://glasgow.social/tags/coding</link>
    <webfeeds:logo>https://glasgow.social/packs/media/images/logo-fe5141d38a25f50068b4c69b77ca1ec8.svg</webfeeds:logo>
    <webfeeds:accentColor>2b90d9</webfeeds:accentColor>
    <item>
      <title>New status by neil</title>
      <guid isPermalink="true">https://glasgow.social/@neil/102489877960955931</guid>
      <link>https://glasgow.social/@neil/102489877960955931</link>
      <pubDate>Tue, 23 Jul 2019 08:43:48 +0000</pubDate>
      <description>&lt;p&gt;Coding from Scratch meetup tonight in &lt;a href="https://glasgow.social/tags/theavalon" 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="https://glasgow.social/tags/coding" class="mention hashtag" rel="tag"&gt;#&lt;span&gt;coding&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>New status by Legomancer</title>
      <guid isPermalink="true">https://glasgow.social/@Legomancer/102464910703794081</guid>
      <link>https://glasgow.social/@Legomancer/102464910703794081</link>
      <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="https://www.jetbrains.com/help/phpstorm/create-new-project-add-remote-server.html" rel="nofollow noopener" target="_blank"&gt;&lt;span class="invisible"&gt;https://www.&lt;/span&gt;&lt;span class="ellipsis"&gt;jetbrains.com/help/phpstorm/cr&lt;/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="https://glasgow.social/tags/coding" class="mention hashtag" rel="tag"&gt;#&lt;span&gt;coding&lt;/span&gt;&lt;/a&gt; &lt;a href="https://glasgow.social/tags/codingfromscratch" class="mention hashtag" rel="tag"&gt;#&lt;span&gt;codingfromscratch&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;</description>
    </item>
 
...

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)

<?php
 
$rss_url = "https://glasgow.social/tags/coding.rss"
 
// 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>";
}
 
 
?>
working_with_rss.1563891265.txt.gz · Last modified: 2019/07/23 14:14 by nmckillop