summaryrefslogtreecommitdiffstats
path: root/3rdparty/fguillot/picofeed/docs/feed-parsing.markdown
blob: 10f20d31a3e6d01d0ed976a91dbe7325f030d7c6 (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
Feed parsing
============

Download and parse a feed
-------------------------

Try this example from a command line script:

```php
<?php

require 'path/to/PicoFeed.php';

use PicoFeed\Reader;

$reader = new Reader;

// Try to discover the XML feed automatically
$reader->download('http://bbc.co.uk/news');

$parser = $reader->getParser();

if ($parser !== false) {

    $feed = $parser->execute();

    if ($feed !== false) {
        echo $feed;
    }
}
```

- The method `getParser()` return `false` when there is something wrong during the download or the feed detection
- The call `$parser->execute()` return `false` when there is a parsing error

In your terminal you will got an output like that:

```
Feed::id = http://www.bbc.co.uk/news/#sa-ns_mchannel=rss&ns_source=PublicRSS20-sa
Feed::title = BBC News - Home
Feed::url = http://www.bbc.co.uk/news/#sa-ns_mchannel=rss&ns_source=PublicRSS20-sa
Feed::date = 1399934742
Feed::language = en-gb
Feed::items = 84 items
----
Item::id = e411a646
Item::title = Nigeria rejects captive girls 'swap'
Item::url = http://www.bbc.co.uk/news/world-africa-27386285#sa-ns_mchannel=rss&ns_source=PublicRSS20-sa
Item::date = 1399933404
Item::language = en-gb
Item::author =
Item::enclosure_url =
Item::enclosure_type =
Item::content = <p>Nigeria insists i... (152 bytes)
----
Item::id = 6c50fcf2
Item::title = Woman tells of Harris 'assaults'
Item::url = http://www.bbc.co.uk/news/uk-27371573#sa-ns_mchannel=rss&ns_source=PublicRSS20-sa
Item::date = 1399908906
Item::language = en-gb
Item::author =
Item::enclosure_url =
Item::enclosure_type =
Item::content = <p>A woman tells the... (142 bytes)
...........
```

This ouput is generated by the magic method `__toString()` of the class `Feed` and `Item`.
All properties are public and they are also available with getter methods:

```php

// Examples for the feed:
echo $feed->getId();              // Unique feed id
echo $feed->getTitle();           // Feed title
echo $feed->getUrl();             // Feed url
echo $feed->getDate();            // Feed last updated date
echo $feed->getLanguage();        // Feed language
echo $feed->getDescription();     // Feed description
echo $feed->getLogo();            // Feed logo (can be a large image, different from icon)
echo $feed->getItems();           // List of items

// Examples for items:
echo $feed->items[0]->getId();
echo $feed->items[0]->getTitle();
echo $feed->items[0]->getUrl();
echo $feed->items[0]->getDate();
echo $feed->items[0]->getLanguage();
echo $feed->items[0]->getAuthor();
echo $feed->items[0]->getEnclosureUrl();
echo $feed->items[0]->getEnclosureType();
echo $feed->items[0]->getContent();
```

Handle HTTP cache
-----------------

To avoid downloading and parsing the feed each time, it's a good idea to handle the HTTP caching:

1. After the first HTTP request, we save somewhere (in a database) the headers Etag and Last-Modified for the next checks
2. If the feed is not modified, we don't need to parse again the feed

Example:

```php
use PicoFeed\Reader;

$reader = new Reader;

// Get last modified infos from previous requests
$lastModified = '...';
$etag = '...';

// Download directly the feed
$resource = $reader->download('http://linuxfr.org/news.atom', $lastModified, $etag);

// Return true is the feed has changed
if ($resource->isModified()) {

    $parser = $reader->getParser();

    if ($parser !== false) {

        $feed = $parser->execute();

        if ($feed !== false) {

            // Save cache infos for the next request
            $lastModified = $resource->getLastModified();
            $etag = $resource->getEtag();
        }
    }
}
```

Use a custom user agent
-----------------------

You have to define a custom configuration for that:

```php
use PicoFeed\Reader;
use PicoFeed\Config;

$config = new Config;
$config->setClientUserAgent('My RSS Reader');

$reader = new Reader($config);
...
```

The complete config parameters are [described here](config.markdown).

Set a custom timezone
---------------------

By default, the timezone used is UTC but you can define a custom timezone for the logging and item parsing.

```php
use PicoFeed\Reader;
use PicoFeed\Config;

$config = new Config;
$config->setTimezone('Europe/Paris');

$reader = new Reader($config);
...
```

[List of supported TimeZones](http://php.net/manual/en/timezones.php)

Disable content filtering
-------------------------

If you want to disable the internal filtering system to use an external library like [HTMLPurifier](http://htmlpurifier.org):

```php
use PicoFeed\Reader;
use PicoFeed\Config;

$config = new Config;
$config->setTimezone('Europe/Paris');
$config->setContentFiltering(false);

$reader = new Reader($config);
...
```

or

```php
use PicoFeed\Reader;

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

$parser = $reader->getParser();

if ($parser !== false) {

    $parser->disableContentFiltering(); // <= Disable content filtering
    $feed = $parser->execute();
    // ...
}
```