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