InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
PHP
Writing Atom Feeds in PHP
PHP

Writing Atom Feeds in PHP

InfinityCoder December 8, 2016

You want to generate Atom feeds from your data. This will allow you to syndicate your content.

Use this class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
class atom1 extends DOMDocument {
    private $ns;
 
    public function __construct($title, $href, $name, $id) {
       parent::__construct();
       $this->formatOutput = true;
 
       $this->ns = 'http://www.w3.org/2005/Atom';
 
       $root = $this->appendChild($this->createElementNS($this->ns, 'feed'));
 
       $root->appendChild($this->createElementNS($this->ns, 'title', $title));
       $link = $root->appendChild($this->createElementNS($this->ns, 'link'));
       $link->setAttribute('href', $href);
       $root->appendChild($this->createElementNS($this->ns, 'updated',
           date(DATE_ATOM)));
       $author = $root->appendChild($this->createElementNS($this->ns,
                                                           'author'));
       $author->appendChild($this->createElementNS($this->ns, 'name', $name));
       $root->appendChild($this->createElementNS($this->ns, 'id', $id));
   }
 
   public function addEntry($title, $link, $summary) {
       $entry = $this->createElementNS($this->ns, 'entry');
       $entry->appendChild($this->createElementNS($this->ns, 'title', $title));
       $entry->appendChild($this->createElementNS($this->ns, 'link', $link));
 
       $id = uniqid('http://example.org/atom/entry/ids/');
       $entry->appendChild($this->createElementNS($this->ns, 'id', $id));
       $entry->appendChild($this->createElementNS($this->ns, 'updated',
           date(DATE_ATOM)));
       $entry->appendChild($this->createElementNS($this->ns, 'summary',
           $summary));
 
       $this->documentElement->appendChild($entry);
   }
}
 
$atom = new atom1('Channel Title', 'http://www.example.org',
                'John Quincy Atom', 'http://example.org/atom/feed/ids/1');
 
$atom->addEntry('Item 1', 'http://www.example.org/item1',
               'Item 1 Description', 'http://example.org/atom/entry/ids/1');
 
$atom->addEntry('Item 2', 'http://www.example.org/item2',
               'Item 2 Description', 'http://example.org/atom/entry/ids/2');
 
print $atom->saveXML();

The atom1 class is structured similar to the rss2 class from Recipe 12.13. Read its Discussion for a more detailed explanation of the overall code structure and DOM extension behavior.

This recipe covers the differences between RSS and Atom and how the class is updated to handle them.
The Atom specification is more complex than RSS. It requires you to place elements inside a namespace and also forces the generation of unique identifiers for a feed and individual items, along with the last updated times for those entries.
Also, though its general structure is similar to RSS, it uses different terminology. The root element is now a feed and an item is now an entry.

You don’t need a feed description, but you do need an author. And inside the entries, the description is a summary.
Last, there is no concept of a channel. Both feed data and entries are located directly under the document element.

Here’s the updated constructor:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public function __construct($title, $href, $name, $id) {
       parent::__construct();
       $this->formatOutput = true;
 
       $this->ns = 'http://www.w3.org/2005/Atom';
 
       $root = $this->appendChild($this->createElementNS($this->ns, 'feed'));
 
       $root->appendChild(
           $this->createElementNS($this->ns, 'title', $title));
       $link = $root->appendChild(
           $this->createElementNS($this->ns, 'link'));
       $link->setAttribute('href', $href);
       $root->appendChild($this->createElementNS(
           $this->ns, 'updated', date(DATE_ATOM)));
       $author = $root->appendChild(
           $this->createElementNS($this->ns, 'author'));
       $author->appendChild(
           $this->createElementNS($this->ns, 'name', $name));
       $root->appendChild(
           $this->createElementNS($this->ns, 'id', $id'));
}

All Atom elements live under the http://www.w3.org/2005/Atom XML namespace. Therefore, all atom1 methods use DOMDocument::createElementNS(), which is the namespace version of DOMDocument::createElement().

The Atom namespace is stored in atom1::ns, so it’s easy to access. The constructor now takes four arguments: title, link, author name, and feed ID.

The title and id are defined similar to RSS channel elements. However, the link is actually set as the href attribute of the link element, and the name is a child of the author element.
Additionally, there is an updated element, which is set to the last update time. In this case, it’s set to the current time and formatted using PHP’s built-in DATE_ATOM constant formatting specification.
The addItem() method is renamed to addEntry() to be consistent with the Atom specification:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public function addEntry($title, $link, $summary, $id) {
       $entry = $this->createElementNS($this->ns, 'entry');
       $entry->appendChild(
          $this->createElementNS($this->ns, 'title', $title));
       $entry->appendChild(
          $this->createElementNS($this->ns, 'link', $link));
       $entry->appendChild(
          $this->createElementNS($this->ns, 'id', $id));
       $entry->appendChild(
          $this->createElementNS($this->ns, 'updated', date(DATE_ATOM)));
       $entry->appendChild(
          $this->createElementNS($this->ns, 'summary', $summary));
 
       $this->documentElement->appendChild($entry);
}

It behaves very similar to its counterpart, with the few additions of new elements, such as id and updated.
Everything comes together like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
$atom = new atom1('Channel Title', 'http://www.example.org',
                'John Quincy Atom', 'http://example.org/atom/feed/ids/1');
 
$atom->addEntry('Item 1', 'http://www.example.org/item1',
              'Item 1 Description', 'http://example.org/atom/entry/ids/1');
 
$atom->addEntry('Item 2', 'http://www.example.org/item2',
              'Item 2 Description', 'http://example.org/atom/entry/ids/2');
 
print $atom->saveXML();
 
<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>Channel Title</title>
  <link href="http://www.example.org"/>
  <updated>2006-10-23T22:33:59-07:00</updated>
  <author>
    <name>John Quincy Atom</name>
  </author>
  <id>http://example.org/atom/feed/ids/1</id>
  <entry>
    <title>Item 1</title>
    <link>http://www.example.org/item1</link>
    <id>http://example.org/atom/entry/ids/1</id>
    <updated>2014-10-23T20:23:32-07:00</updated>
    <summary>Item 1 Description</summary>
  </entry>
  <entry>
    <title>Item 2</title>
    <link>http://www.example.org/item2</link>
    <id>http://example.org/atom/entry/ids/2</id>
    <updated>2014-10-23T21:53:44-07:00</updated>
    <summary>Item 2 Description</summary>
  </entry>
</feed>

Like the rss2 class, atom1 implements only a small subset of the full specification. It’s enough to generate a valid feed, but if you need to do more, you will need to extend the class.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Generating Biased Random Numbers in PHP
You want to generate random numbers, but you want these …

Generating Biased Random Numbers in PHP

Modifying Data in an SQL Database in PHP
You want to add, remove, or change data in an …

Modifying Data in an SQL Database in PHP

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

    Categories

    • Bash
    • PHP
    • Python
    • Uncategorized

    InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

    About Us

    Start learning your desired programming language with InfinityQuest.com.

    On our website you can access any tutorial that you want with video and code examples.

    We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

    Popular Tags

    binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

    Archives

    • June 2017
    • April 2017
    • February 2017
    • January 2017
    • December 2016
    • November 2016
    Copyright © 2019 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
    Programming Tutorials | Sitemap