Chris@0
|
1 # Consuming RSS Feeds
|
Chris@0
|
2
|
Chris@0
|
3 ## Reading a feed
|
Chris@0
|
4
|
Chris@0
|
5 To read an RSS feed, pass its URL to `Zend\Feed\Reader\Reader::import()`:
|
Chris@0
|
6
|
Chris@0
|
7 ```php
|
Chris@0
|
8 $channel = Zend\Feed\Reader\Reader::import('http://rss.example.com/channelName');
|
Chris@0
|
9 ```
|
Chris@0
|
10
|
Chris@0
|
11 > ## Importing requires an HTTP client
|
Chris@0
|
12 >
|
Chris@0
|
13 > To import a feed, you will need to have an [HTTP client](zend.feed.http-clients)
|
Chris@0
|
14 > available.
|
Chris@0
|
15 >
|
Chris@0
|
16 > If you are not using zend-http, you will need to inject `Reader` with the HTTP
|
Chris@0
|
17 > client. See the [section on providing a client to Reader](http-clients.md#providing-a-client-to-reader).
|
Chris@0
|
18
|
Chris@0
|
19 If any errors occur fetching the feed, a
|
Chris@0
|
20 `Zend\Feed\Reader\Exception\RuntimeException` will be thrown.
|
Chris@0
|
21
|
Chris@0
|
22 ## Get properties
|
Chris@0
|
23
|
Chris@0
|
24 Once you have a feed object, you can access any of the standard RSS channel
|
Chris@0
|
25 properties via the various instance getter methods:
|
Chris@0
|
26
|
Chris@0
|
27 ```php
|
Chris@0
|
28 echo $channel->getTitle();
|
Chris@0
|
29 echo $channel->getAuthor();
|
Chris@0
|
30 // etc.
|
Chris@0
|
31 ```
|
Chris@0
|
32
|
Chris@0
|
33 If channel properties have attributes, the getter method will return a key/value
|
Chris@0
|
34 pair, where the key is the attribute name, and the value is the attribute value.
|
Chris@0
|
35
|
Chris@0
|
36 ```php
|
Chris@0
|
37 $author = $channel->getAuthor();
|
Chris@0
|
38 echo $author['name'];
|
Chris@0
|
39 ```
|
Chris@0
|
40
|
Chris@0
|
41 Most commonly, you'll want to loop through the feed and do something with its
|
Chris@0
|
42 entries. `Zend\Feed\Reader\Feed\Rss` internally converts all entries to a
|
Chris@0
|
43 `Zend\Feed\Reader\Entry\Rss` instance. Entry properties, similarly to channel
|
Chris@0
|
44 properties, can be accessed via getter methods, such as `getTitle`,
|
Chris@0
|
45 `getDescription`, etc.
|
Chris@0
|
46
|
Chris@0
|
47 An example of printing all titles of articles in a channel is:
|
Chris@0
|
48
|
Chris@0
|
49 ```php
|
Chris@0
|
50 foreach ($channel as $item) {
|
Chris@0
|
51 echo $item->getTitle() . "\n";
|
Chris@0
|
52 }
|
Chris@0
|
53 ```
|
Chris@0
|
54
|
Chris@0
|
55 If you are not familiar with RSS, here are the standard elements you can expect
|
Chris@0
|
56 to be available in an RSS channel and in individual RSS items (entries).
|
Chris@0
|
57
|
Chris@0
|
58 Required channel elements:
|
Chris@0
|
59
|
Chris@0
|
60 - `title`: The name of the channel.
|
Chris@0
|
61 - `link`: The URL of the web site corresponding to the channel.
|
Chris@0
|
62 - `description`: A sentence (or more) describing the channel.
|
Chris@0
|
63
|
Chris@0
|
64 Common optional channel elements:
|
Chris@0
|
65
|
Chris@0
|
66 - `pubDate`: The publication date of this set of content, in RFC 822 date
|
Chris@0
|
67 format.
|
Chris@0
|
68 - `language`: The language the channel is written in.
|
Chris@0
|
69 - `category`: One or more (specified by multiple tags) categories the channel
|
Chris@0
|
70 belongs to.
|
Chris@0
|
71
|
Chris@0
|
72 RSS `<item>` elements do not have any strictly required elements. However,
|
Chris@0
|
73 either `title` or `description` must be present.
|
Chris@0
|
74
|
Chris@0
|
75 Common item elements:
|
Chris@0
|
76
|
Chris@0
|
77 - `title`: The title of the item.
|
Chris@0
|
78 - `link`: The URL of the item.
|
Chris@0
|
79 - `description`: A synopsis of the item.
|
Chris@0
|
80 - `author`: The author's email address.
|
Chris@0
|
81 - `category`: One more categories that the item belongs to.
|
Chris@0
|
82 - `comments`: URL of comments relating to this item.
|
Chris@0
|
83 - `pubDate`: The date the item was published, in RFC 822 date format.
|
Chris@0
|
84
|
Chris@0
|
85 In your code you can always test to see if an element is non-empty by calling
|
Chris@0
|
86 the getter:
|
Chris@0
|
87
|
Chris@0
|
88 ```php
|
Chris@0
|
89 if ($item->getPropname()) {
|
Chris@0
|
90 // ... proceed.
|
Chris@0
|
91 }
|
Chris@0
|
92 ```
|
Chris@0
|
93
|
Chris@0
|
94 Where relevant, `Zend\Feed` supports a number of common RSS extensions including
|
Chris@0
|
95 Dublin Core, Atom (inside RSS); the Content, Slash, Syndication,
|
Chris@0
|
96 Syndication/Thread extensions; as well as several others.
|
Chris@0
|
97
|
Chris@0
|
98 Please see the official [RSS 2.0 specification](http://cyber.law.harvard.edu/rss/rss.html)
|
Chris@0
|
99 for further information.
|