rss-feed-generator

Gesponsorde koppelingen

PHP script bestanden

  1. rss-feed-generator

« Lees de omschrijving en reacties

Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
<?php

/**
 * Build a RSS Feed folowing the RSS 2.0.10 specification at:
 * http://www.rssboard.org/rss-specification
 *
 * @author Boaz den Besten
 * @copyright GNU/GPL
 * @version @version@
 */

class RssFeed extends DOMDocument {
    
    /**
     * Create a rss feed
     *
     * @param String $title The name of the channel. It's how people refer to your service. If you have an HTML website that contains the same information as your RSS file, the title of your channel should be the same as the title of your website.
     * @param String $link The URL to the HTML website corresponding to the channel.
     * @param String $description Phrase or sentence describing the channel.
     */

    public function __construct($title, $link, $description){
        parent::__construct();
        
        $root = $this->appendChild($this->createElement('rss'));
        $root->setAttribute('version', '2.0');

        $channel = $root->appendChild($this->createElement('channel'));        
        $channel->appendChild(new DOMElement('title', $title));
        $channel->appendChild(new DOMElement('description', $description));
        
        if(!filter_var($link, FILTER_VALIDATE_URL)) {
            throw new RssFeedException($link.' is not a valid url.');
        }

        $channel->appendChild(new DOMElement('link', $link));
    }

    
    /**
     * The language the channel is written in.
     *
     * @param String $language
     */

    public function setLanguage($language){
        if(!filter_var($language, FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => '#^([a-z]{2,3}|[a-z]{2}-[a-z]{2})$#')))) {
            throw new RssFeedException($language.' is not a valid RSS Language Code.');
        }

        $this->getElementsByTagName('channel')->item(0)->appendChild(new DOMElement('language', $language));
    }

    
    /**
     * Copyright notice for content in the channel.
     *
     * @param String $copyright
     */

    public function setCopyright($copyright){
        $this->getElementsByTagName('channel')->item(0)->appendChild(new DOMElement('copyright', $copyright));
    }

    
    /**
     * Email address for person responsible for technical issues relating to channel.
     *
     * @param String $webmaster
     */

    public function setWebMaster($webmaster){
        $this->getElementsByTagName('channel')->item(0)->appendChild(new DOMElement('webMaster', $webmaster));
    }

    
    /**
     * The last time the content of the channel changed.
     *
     * @param DateTime $lastBuildDate
     */

    public function setLastBuildDate(DateTime $lastBuildDate=null){
        $this->getElementsByTagName('channel')->item(0)->appendChild(new DOMElement('lastBuildDate', $lastBuildDate->format(DATE_RSS)));
    }

    
    /**
     * Email address for person responsible for editorial content.
     *
     * @param String $managingEditor
     */

    public function setManagingEditor($managingEditor){
        $this->getElementsByTagName('channel')->item(0)->appendChild(new DOMElement('managingEditor', $managingEditor));
    }

    
    /**
     * The publication date for the content in the channel.
     *
     * @param DateTime $pubDate
     */

    public function setPubDate(DateTime $pubDate){
        $this->getElementsByTagName('channel')->item(0)->appendChild(new DOMElement('pubDate', $pubDate->format(DATE_RSS)));
    }

    
    /**
     * Specify one or more categories that the channel belongs to.
     *
     * @param String $category
     * @param String $domain
     */

    public function setCategory($category, $domain=null) {
         $category = $this->getElementsByTagName('channel')->item(0)->appendChild(new DOMElement('category', $category));
         if(!is_null($domain)) {
             $category->setAttribute('domain', $domain);
         }
    }

    
    /**
     * A string indicating the program used to generate the channel.
     *
     * @param String $generator
     */

    public function setGenerator($generator){
        $this->getElementsByTagName('channel')->item(0)->appendChild(new DOMElement('generator', $generator));
    }

    
    /**
     * A URL that points to the documentation for the format used in the RSS file.
     *
     * @param String $docs
     */

    public function setDocs($docs) {
        if(!filter_var($docs, FILTER_VALIDATE_URL)) {
            throw new RssFeedException($docs.' is not a valid url.');
        }

        $this->getElementsByTagName('channel')->item(0)->appendChild(new DOMElement('docs', $docs));
    }

    
    /**
     * Allows processes to register with a cloud to be notified of updates to the channel, implementing a lightweight publish-subscribe protocol for RSS feeds.
     *
     * @param String $domain
     * @param String $port
     * @param String $path
     * @param String $registerProcedure
     * @param String $protocol
     */

    public function setCloud($domain, $port, $path, $registerProcedure, $protocol) {
        $cloud = $this->getElementsByTagName('channel')->item(0)->appendChild(new DOMElement('cloud'));
        $cloud->setAttribute('domain', $domain);
        $cloud->setAttribute('port', $port);
        $cloud->setAttribute('path', $path);
        $cloud->setAttribute('registerProcedure', $registerProcedure);
        $cloud->setAttribute('protocol', $protocol);
    }

    
    /**
     * ttl stands for time to live. It's a number of minutes that indicates how long a channel can be cached before refreshing from the source.
     *
     * @param Integer $ttl
     */

    public function setTtl($ttl){
        if(!is_integer($ttl)) {
            throw new RssFeedException($ttl.' is not a valid integer.');
        }

        $this->getElementsByTagName('channel')->item(0)->appendChild(new DOMElement('ttl', $ttl));
    }

    
    /**
     * Specifies a GIF, JPEG or PNG image that can be displayed with the channel.
     *
     * @param String $url
     * @param Integer $width
     * @param Integer $height
     * @param String $description
     */

    public function setImage($url, $width=null, $height=null, $description=null) {
        if(!filter_var($url, FILTER_VALIDATE_URL) || !filter_var($url, FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => '#\.(jpg|jpeg|png|gif)$#')))) {
            throw new RssFeedException($url.' is not a proper image url. (only jpeg, png and gif are supported.)');
        }

        
        $image = $this->getElementsByTagName('channel')->item(0)->appendChild(new DOMElement('image'));
        $image->appendChild(new DOMElement('url', $url));
        $image->appendChild(new DOMElement('title', $this->getElementsByTagName('title')->item(0)->nodeValue));
        $image->appendChild(new DOMElement('link', $this->getElementsByTagName('link')->item(0)->nodeValue));
        
        if(!is_null($width) && is_integer($width)) {
            $image->appendChild(new DOMElement('width', $width));
        }

        
        if(!is_null($height) && is_integer($height)) {
            $image->appendChild(new DOMElement('height', $height));
        }

        
        if(!is_null($description)) {
            $image->appendChild(new DOMElement('description', $description));
        }
    }

    
    /**
     * The PICS rating for the channel.
     *
     * @param String $rating
     */

    public function setRating($rating){
        $this->getElementsByTagName('channel')->item(0)->appendChild(new DOMElement('rating', $rating));
    }

    
    /**
     * Specifies a text input box that can be displayed with the channel.
     *
     * @param String $title
     * @param String $description
     * @param String $name
     * @param String $link
     */

    public function setTextInput($title, $description, $name, $link) {
        if(!filter_var($link, FILTER_VALIDATE_URL)) {
            throw new RssFeedException($link.' is not a proper url.');
        }

        $textInput = $this->getElementsByTagName('channel')->item(0)->appendChild(new DOMElement('textInput'));
        $textInput->appendChild(new DOMElement('title', $title));
        $textInput->appendChild(new DOMElement('description', $description));
        $textInput->appendChild(new DOMElement('name', $name));
        $textInput->appendChild(new DOMElement('link', $link));
    }

    
    /**
     * A hint for aggregators telling them which hours they can skip.
     *
     * @param Array<Integer> $skipHours
     */

    public function setSkipHours($skipHours) {
        if(!is_array($skipHours) || count($skipHours) > 24) {
            throw new RssFeedException('setSkipHours expect an array with 24 elements max.');
        }

        
        $skipHoursEl = $this->getElementsByTagName('channel')->item(0)->appendChild(new DOMElement('skipHours'));
        
        foreach($skipHours as $skipHour) {
            if(!filter_var($skipHour, FILTER_VALIDATE_INT, array('options' => array('min_range' => 0, 'max_range' => 23)))) {
                throw new RssFeedException('each skipHours array element sould be an integer between 0 and 23 (hour GMT).');
            }

            $skipHoursEl->appendChild(new DOMElement('hour', $skipHour));
        }
    }

    
    /**
     * A hint for aggregators telling them which days they can skip.
     *
     * @param Array<String> $skipDays
     */

    public function setSkipDays($skipDays) {
        if(!is_array($skipDays) || count($skipDays) > 7) {
            throw new RssFeedException('setSkipDays expect an array with 7 elements max.');
        }

        
        $skipDaysEl = $this->getElementsByTagName('channel')->item(0)->appendChild(new DOMElement('skipDays'));
        
        foreach($skipDays as $skipDay) {
            if(!filter_var($skipDay, FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => '#^(Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday)$#')))) {
                throw new RssFeedException('each skipDays array element sould be an String with the english name of a day starting with a capital.');
            }

            $skipDaysEl->appendChild(new DOMElement('day', $skipDay));
        }
    }

    
    /**
     * Creates an ads a new instance of RssFeedItem to the RssFeed
     *
     * @return RssFeedItem
     */

    public function createFeedItem() {
        return $this->getElementsByTagName('channel')->item(0)->appendChild(new RssFeedItem());
    }
    
}


/**
 * A Rss Feed item-element folowing the RSS 2.0.10 specification at:
 * http://www.rssboard.org/rss-specification
 *
 * @author Boaz den Besten
 * @copyright GNU/GPL
 * @version @version@
 */

class RssFeedItem extends DOMElement {

    /**
     * Create a new RssFeedItem
     *
     * If u use this constructor a readonly RssFeedItem will becreated,
     * use RssFeed::createFeedItem to create and add a new item to the feed.
     */

    public function __construct() {
       parent::__construct('item');
    }

    
    /**
     * The title of the item.
     *
     * @param String $title
     */

    public function setTitle($title) {
        $this->appendChild(new DOMElement('title', $title));
    }

    
    /**
     * The URL of the item.
     *
     * @param String $link
     */

    public function setLink($link) {
        if(!filter_var($link, FILTER_VALIDATE_URL)) {
            throw new RssFeedException($link.' is not a valid url.');
        }

        $this->appendChild(new DOMElement('link', $link));
    }

    
    /**
     * The item synopsis.
     *
     * @param String/DOMCDATASection $description
     */

    public function setDescription($description) {
        if($description instanceof DOMCDATASection) {
            $descriptionEl = $this->appendChild(new DOMElement('description'));
            $descriptionEl->appendChild($description);
        }
else {
            $this->appendChild(new DOMElement('description', $description));
        }
    }

    
    /**
     * Email address of the author of the item.
     *
     * @param String $author
     */

    public function setAuthor($author) {
        $this->appendChild(new DOMElement('author', $author));
    }

    
    /**
     * Indicates when the item was published.
     *
     * @param DateTime $pubDate
     */

    public function setPubDate(DateTime $pubDate) {
        $this->appendChild(new DOMElement('pubDate', $pubDate->format(DATE_RSS)));
    }

    
    /**
     * Includes the item in one or more categories.
     *
     * @param String $category
     * @param String $domain
     */

    public function setCategory($category, $domain=null) {
         $category = $this->appendChild(new DOMElement('category', $category));
         if(!is_null($domain)) {
             $category->setAttribute('domain', $domain);
         }
    }

    
    /**
     * URL of a page for comments relating to the item.
     *
     * @param String $comments
     */

    public function setComments($comments) {
        if(!filter_var($comments, FILTER_VALIDATE_URL)) {
            throw new RssFeedException($comments.' is not a valid url.');
        }

        $this->appendChild(new DOMElement('comments', $comments));
    }

    
    /**
     * Describes a media object that is attached to the item.
     *
     * @param String $url
     * @param Integer $length
     * @param String $type
     */

    public function setEnclosure($url, $length, $type) {
        if(!is_integer($length)) {
            throw new RssFeedException('length should be an integer representing the bit size of the enclosure.');
        }

        
        $enclosure = $this->appendChild(new DOMElement('enclosure'));
        $enclosure->setAttribute('url', $url);
        $enclosure->setAttribute('length', $length);
        $enclosure->setAttribute('type', $type);
    }

    
    /**
     * A string that uniquely identifies the item.
     *
     * @param String $guid
     * @param Boolean $isPermaLink
     */

    public function setGuid($guid, $isPermaLink=false) {
        if($isPermaLink && !filter_var($guid, FILTER_VALIDATE_URL)) {
            throw new RssFeedException('If isPermaLink is set true, guid should be an url.');
        }

        
        $guid = $this->appendChild(new DOMElement('guid', $guid));
        
        if($isPermaLink) {
            $guid->setAttribute('isPermaLink', 'true');
        }
    }

    
    /**
     * The RSS channel that the item came from.
     *
     * @param String $source
     * @param String $url
     */

    public function setSource($source, $url) {
        if(!filter_var($url, FILTER_VALIDATE_URL)) {
            throw new RssFeedException($url.' is not a valid url.');
        }

        
        $source = $this->appendChild(new DOMElement('source', $source));
        $source->setAttribute('url', $url);
    }
    
}


/**
 * Special exception class for RssFeed* classes
 *
 * @author Boaz den Besten
 * @copyright GNU/GPL
 * @version @version@
 */

class RssFeedException extends Exception {
    
}


?>

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.