Chris@0
|
1 # Importing Feeds
|
Chris@0
|
2
|
Chris@0
|
3 `Zend\Feed` enables developers to retrieve feeds via `Zend\Feader\Reader`. If
|
Chris@0
|
4 you know the URI of a feed, use the `Zend\Feed\Reader\Reader::import()` method
|
Chris@0
|
5 to consume it:
|
Chris@0
|
6
|
Chris@0
|
7 ```php
|
Chris@0
|
8 $feed = Zend\Feed\Reader\Reader::import('http://feeds.example.com/feedName');
|
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 You can also use `Zend\Feed\Reader\Reader` to fetch the contents of a feed from
|
Chris@0
|
20 a file or the contents of a PHP string variable:
|
Chris@0
|
21
|
Chris@0
|
22 ```php
|
Chris@0
|
23 // importing a feed from a text file
|
Chris@0
|
24 $feedFromFile = Zend\Feed\Reader\Reader::importFile('feed.xml');
|
Chris@0
|
25
|
Chris@0
|
26 // importing a feed from a PHP string variable
|
Chris@0
|
27 $feedFromPHP = Zend\Feed\Reader\Reader::importString($feedString);
|
Chris@0
|
28 ```
|
Chris@0
|
29
|
Chris@0
|
30 In each of the examples above, an object of a class that extends
|
Chris@0
|
31 `Zend\Feed\Reader\Feed\AbstractFeed` is returned upon success, depending on the
|
Chris@0
|
32 type of the feed. If an RSS feed were retrieved via one of the import methods
|
Chris@0
|
33 above, then a `Zend\Feed\Reader\Feed\Rss` object would be returned. On the other
|
Chris@0
|
34 hand, if an Atom feed were imported, then a `Zend\Feed\Reader\Feed\Atom` object
|
Chris@0
|
35 is returned. The import methods will also throw a
|
Chris@0
|
36 `Zend\Feed\Exception\Reader\RuntimeException` object upon failure, such as an
|
Chris@0
|
37 unreadable or malformed feed.
|
Chris@0
|
38
|
Chris@0
|
39 ## Dumping the contents of a feed
|
Chris@0
|
40
|
Chris@0
|
41 To dump the contents of a `Zend\Feed\Reader\Feed\AbstractFeed` instance, you may
|
Chris@0
|
42 use the `saveXml()` method.
|
Chris@0
|
43
|
Chris@0
|
44 ```php
|
Chris@0
|
45 assert($feed instanceof Zend\Feed\Reader\Feed\AbstractFeed);
|
Chris@0
|
46
|
Chris@0
|
47 // dump the feed to standard output
|
Chris@0
|
48 print $feed->saveXml();
|
Chris@0
|
49 ```
|