annotate vendor/zendframework/zend-feed/doc/book/intro.md @ 2:92f882872392

Trusted hosts, + remove migration modules
author Chris Cannam
date Tue, 05 Dec 2017 09:26:43 +0000
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 # Introduction
Chris@0 2
Chris@0 3 `Zend\Feed` provides functionality for consuming RSS and Atom feeds. It provides
Chris@0 4 a natural syntax for accessing elements of feeds, feed attributes, and entry
Chris@0 5 attributes. `Zend\Feed` also has extensive support for modifying feed and entry
Chris@0 6 structure with the same natural syntax, and turning the result back into XML.
Chris@0 7 In the future, this modification support could provide support for the Atom
Chris@0 8 Publishing Protocol.
Chris@0 9
Chris@0 10 `Zend\Feed` consists of `Zend\Feed\Reader` for reading RSS and Atom feeds,
Chris@0 11 `Zend\Feed\Writer` for writing RSS and Atom feeds, and `Zend\Feed\PubSubHubbub`
Chris@0 12 for working with Hub servers. Furthermore, both `Zend\Feed\Reader` and
Chris@0 13 `Zend\Feed\Writer` support extensions which allows for working with additional
Chris@0 14 data in feeds, not covered in the core API but used in conjunction with RSS and
Chris@0 15 Atom feeds.
Chris@0 16
Chris@0 17 In the example below, we demonstrate a simple use case of retrieving an RSS feed
Chris@0 18 and saving relevant portions of the feed data to a simple PHP array, which could
Chris@0 19 then be used for printing the data, storing to a database, etc.
Chris@0 20
Chris@0 21 > ## RSS optional properties
Chris@0 22 >
Chris@0 23 > Many *RSS* feeds have different channel and item properties available. The
Chris@0 24 > *RSS* specification provides for many optional properties, so be aware of this
Chris@0 25 > when writing code to work with *RSS* data. `Zend\Feed` supports all optional
Chris@0 26 > properties of the core *RSS* and *Atom* specifications.
Chris@0 27
Chris@0 28 ## Reading RSS Feed Data
Chris@0 29
Chris@0 30 ```php
Chris@0 31 // Fetch the latest Slashdot headlines
Chris@0 32 try {
Chris@0 33 $slashdotRss =
Chris@0 34 Zend\Feed\Reader\Reader::import('http://rss.slashdot.org/Slashdot/slashdot');
Chris@0 35 } catch (Zend\Feed\Reader\Exception\RuntimeException $e) {
Chris@0 36 // feed import failed
Chris@0 37 echo "Exception caught importing feed: {$e->getMessage()}\n";
Chris@0 38 exit;
Chris@0 39 }
Chris@0 40
Chris@0 41 // Initialize the channel/feed data array
Chris@0 42 $channel = [
Chris@0 43 'title' => $slashdotRss->getTitle(),
Chris@0 44 'link' => $slashdotRss->getLink(),
Chris@0 45 'description' => $slashdotRss->getDescription(),
Chris@0 46 'items' => [],
Chris@0 47 ];
Chris@0 48
Chris@0 49 // Loop over each channel item/entry and store relevant data for each
Chris@0 50 foreach ($slashdotRss as $item) {
Chris@0 51 $channel['items'][] = [
Chris@0 52 'title' => $item->getTitle(),
Chris@0 53 'link' => $item->getLink(),
Chris@0 54 'description' => $item->getDescription(),
Chris@0 55 ];
Chris@0 56 }
Chris@0 57 ```
Chris@0 58
Chris@0 59 Your `$channel` array now contains the basic meta-information for the RSS
Chris@0 60 channel and all items that it contained. The process is identical for Atom
Chris@0 61 feeds since `Zend\Feed` provides a common feed API; i.e. all getters and
Chris@0 62 setters are the same regardless of feed format.