summaryrefslogtreecommitdiffstats
path: root/vendor/fguillot/picofeed/docs/feed-parsing.markdown
blob: 22f84339bd0b79a6a4c362d213c2b8233ed2c14b (plain)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
Feed parsing
============

Parsing a subscription
----------------------

```php
use PicoFeed\Reader\Reader;
use PicoFeed\PicoFeedException;

try {

    $reader = new Reader;

    // Return a resource
    $resource = $reader->download('https://linuxfr.org/news.atom');

    // Return the right parser instance according to the feed format
    $parser = $reader->getParser(
        $resource->getUrl(),
        $resource->getContent(),
        $resource->getEncoding()
    );

    // Return a Feed object
    $feed = $parser->execute();

    // Print the feed properties with the magic method __toString()
    echo $feed;
}
catch (PicoFeedException $e) {
    // Do Something...
}
```

- The Reader class is the entry point for feed reading
- The method `download()` fetch the remote content and return a resource, an instance of `PicoFeed\Client\Client`
- The method `getParser()` returns a Parser instance according to the feed format Atom, Rss 2.0...
- The parser itself returns a `Feed` object that contains feed and item properties

Output:

```bash
Feed::id = tag:linuxfr.org,2005:/news
Feed::title = LinuxFr.org : les dépêches
Feed::url = http://linuxfr.org/news
Feed::date = 1415138079
Feed::language = en-US
Feed::description =
Feed::logo =
Feed::items = 15 items
----
Item::id = 38d8f48284fb03940cbb3aff9101089b81e44efb1281641bdd7c3e7e4bf3b0cd
Item::title = openSUSE 13.2 : nouvelle version du caméléon disponible !
Item::url = http://linuxfr.org/news/opensuse-13-2-nouvelle-version-du-cameleon-disponible
Item::date = 1415122640
Item::language = en-US
Item::author = Syvolc
Item::enclosure_url =
Item::enclosure_type =
Item::content = 18307 bytes
----
Item::id = d0ebddc90bfc3f109f9be00a3bb0b4a770af7a647cdc88454fe15d79168e0dea
Item::title = Fuzix OS, parce que les petites choses sont belles
Item::url = http://linuxfr.org/news/fuzix-os-parce-que-les-petites-choses-sont-belles
Item::date = 1415112167
Item::language = en-US
Item::author = Thomas DEBESSE
Item::enclosure_url =
Item::enclosure_type =
Item::content = 6104 bytes
....
```

Get the list of available subscriptions for a website
-----------------------------------------------------

The example below will returns all available subscriptions for the website:

```php
use PicoFeed\Reader\Reader;

try {

    $reader = new Reader;
    $resource = $reader->download('http://www.cnn.com');

    $feeds = $reader->find(
        $resource->getUrl(),
        $resource->getContent()
    );

    print_r($feeds);
}
catch (PicoFeedException $e) {
    // Do something...
}
```

Output:

```php
Array
(
    [0] => http://rss.cnn.com/rss/cnn_topstories.rss
    [1] => http://rss.cnn.com/rss/cnn_latest.rss
)
```

Feed discovery and parsing
--------------------------

This example will discover automatically the subscription and parse the feed:

```php
try {

    $reader = new Reader;
    $resource = $reader->discover('http://linuxfr.org');

    $parser = $reader->getParser(
        $resource->getUrl(),
        $resource->getContent(),
        $resource->getEncoding()
    );

    $feed = $parser->execute();
    echo $feed;
}
catch (PicoFeedException $e) {
}
```

HTTP caching
------------

TODO


Feed and item properties
------------------------

```php
// Feed object
$feed->getId();              // Unique feed id
$feed->getTitle();           // Feed title
$feed->getUrl();             // Website url
$feed->getDate();            // Feed last updated date
$feed->getLanguage();        // Feed language
$feed->getDescription();     // Feed description
$feed->getLogo();            // Feed logo (can be a large image, different from icon)
$feed->getItems();           // List of item objects

// Item object
$feed->items[0]->getId();                      // Item unique id (hash)
$feed->items[0]->getTitle();                   // Item title
$feed->items[0]->getUrl();                     // Item url
$feed->items[0]->getDate();                    // Item published date (timestamp)
$feed->items[0]->getLanguage();                // Item language
$feed->items[0]->getAuthor();                  // Item author
$feed->items[0]->getEnclosureUrl();            // Enclosure url
$feed->items[0]->getEnclosureType();           // Enclosure mime-type (audio/mp3, image/png...)
$feed->items[0]->getContent();                 // Item content (filtered or raw)
```