33 | * @link http://www.ajaxray.com/projects/rss
34 | */
35 | class Item
36 | {
37 | /**
38 | * Collection of feed item elements
39 | */
40 | private $elements = array();
41 |
42 | /**
43 | * Contains the format of this feed.
44 | */
45 | private $version;
46 |
47 | /**
48 | * Is used as a suffix when multiple elements have the same name.
49 | **/
50 | private $_cpt = 0;
51 |
52 | /**
53 | * Constructor
54 | *
55 | * @param constant (RSS1/RSS2/ATOM) RSS2 is default.
56 | */
57 | public function __construct($version = Feed::RSS2)
58 | {
59 | $this->version = $version;
60 | }
61 |
62 | /**
63 | * Return an unique number
64 | *
65 | * @access private
66 | * @return int
67 | **/
68 | private function cpt()
69 | {
70 | return $this->_cpt++;
71 | }
72 |
73 | /**
74 | * Add an element to elements array
75 | *
76 | * @access public
77 | * @param string The tag name of an element
78 | * @param string The content of tag
79 | * @param array Attributes (if any) in 'attrName' => 'attrValue' format
80 | * @param boolean Specifies if an already existing element is overwritten.
81 | * @param boolean Specifies if multiple elements of the same name are allowed.
82 | * @return self
83 | */
84 | public function addElement($elementName, $content, $attributes = null, $overwrite = FALSE, $allowMultiple = FALSE)
85 | {
86 | $key = $elementName;
87 |
88 | // return if element already exists & if overwriting is disabled
89 | // & if multiple elements are not allowed.
90 | if (isset($this->elements[$elementName]) && !$overwrite) {
91 | if (!$allowMultiple)
92 | return;
93 |
94 | $key .= '-' . $this->cpt();
95 | }
96 |
97 | $this->elements[$key]['name'] = $elementName;
98 | $this->elements[$key]['content'] = $content;
99 | $this->elements[$key]['attributes'] = $attributes;
100 |
101 | return $this;
102 | }
103 |
104 | /**
105 | * Set multiple feed elements from an array.
106 | * Elements which have attributes cannot be added by this method
107 | *
108 | * @access public
109 | * @param array array of elements in 'tagName' => 'tagContent' format.
110 | * @return self
111 | */
112 | public function addElementArray($elementArray)
113 | {
114 | if (!is_array($elementArray))
115 | return;
116 |
117 | foreach ($elementArray as $elementName => $content) {
118 | $this->addElement($elementName, $content);
119 | }
120 |
121 | return $this;
122 | }
123 |
124 | /**
125 | * Return the collection of elements in this feed item
126 | *
127 | * @access public
128 | * @return array All elements of this item.
129 | */
130 | public function getElements()
131 | {
132 | return $this->elements;
133 | }
134 |
135 | /**
136 | * Return the type of this feed item
137 | *
138 | * @access public
139 | * @return string The feed type, as defined in Feed.php
140 | */
141 | public function getVersion()
142 | {
143 | return $this->version;
144 | }
145 |
146 | // Wrapper functions ------------------------------------------------------
147 |
148 | /**
149 | * Set the 'description' element of feed item
150 | *
151 | * @access public
152 | * @param string The content of 'description' or 'summary' element
153 | * @return self
154 | */
155 | public function setDescription($description)
156 | {
157 | $tag = ($this->version == Feed::ATOM) ? 'summary' : 'description';
158 |
159 | return $this->addElement($tag, $description);
160 | }
161 |
162 | /**
163 | * Set the 'content' element of the feed item
164 | * For ATOM feeds only
165 | *
166 | * @access public
167 | * @param string Content for the item (i.e., the body of a blog post).
168 | * @return self
169 | */
170 | public function setContent($content)
171 | {
172 | if ($this->version != Feed::ATOM)
173 | die('The content element is supported in ATOM feeds only.');
174 |
175 | return $this->addElement('content', $content, array('type' => 'html'));
176 | }
177 |
178 | /**
179 | * Set the 'title' element of feed item
180 | *
181 | * @access public
182 | * @param string The content of 'title' element
183 | * @return self
184 | */
185 | public function setTitle($title)
186 | {
187 | return $this->addElement('title', $title);
188 | }
189 |
190 | /**
191 | * Set the 'date' element of the feed item.
192 | *
193 | * The value of the date parameter can be either an instance of the
194 | * DateTime class, an integer containing a UNIX timestamp or a string
195 | * which is parseable by PHP's 'strtotime' function.
196 | *
197 | * @access public
198 | * @param DateTime|int|string Date which should be used.
199 | * @return self
200 | */
201 | public function setDate($date)
202 | {
203 | if (!is_numeric($date)) {
204 | if ($date instanceof DateTime)
205 | $date = $date->getTimestamp();
206 | else {
207 | $date = strtotime($date);
208 |
209 | if ($date === FALSE)
210 | die('The given date string was not parseable.');
211 | }
212 | } elseif ($date < 0)
213 | die('The given date is not an UNIX timestamp.');
214 |
215 | if ($this->version == Feed::ATOM) {
216 | $tag = 'updated';
217 | $value = date(\DATE_ATOM, $date);
218 | } elseif ($this->version == Feed::RSS2) {
219 | $tag = 'pubDate';
220 | $value = date(\DATE_RSS, $date);
221 | } else {
222 | $tag = 'dc:date';
223 | $value = date("Y-m-d", $date);
224 | }
225 |
226 | return $this->addElement($tag, $value);
227 | }
228 |
229 | /**
230 | * Set the 'link' element of feed item
231 | *
232 | * @access public
233 | * @param string The content of 'link' element
234 | * @return void
235 | */
236 | public function setLink($link)
237 | {
238 | if ($this->version == Feed::RSS2 || $this->version == Feed::RSS1) {
239 | $this->addElement('link', $link);
240 | } else {
241 | $this->addElement('link','',array('href'=>$link));
242 | $this->addElement('id', Feed::uuid($link,'urn:uuid:'));
243 | }
244 |
245 | return $this;
246 | }
247 |
248 | /**
249 | * Attach a external media to the feed item.
250 | * Not supported in RSS 1.0 feeds.
251 | *
252 | * See RFC 4288 for syntactical correct MIME types.
253 | *
254 | * Note that you should avoid the use of more than one enclosure in one item,
255 | * since some RSS aggregators don't support it.
256 | *
257 | * @access public
258 | * @param string The URL of the media.
259 | * @param integer The length of the media.
260 | * @param string The MIME type attribute of the media.
261 | * @param boolean Specifies, if multiple enclosures are allowed
262 | * @return self
263 | * @link https://tools.ietf.org/html/rfc4288
264 | */
265 | public function addEnclosure($url, $length, $type, $multiple = TRUE)
266 | {
267 | if ($this->version == Feed::RSS1)
268 | die('Media attachment is not supported in RSS1 feeds.');
269 |
270 | // the length parameter should be set to 0 if it can't be determined
271 | // see http://www.rssboard.org/rss-profile#element-channel-item-enclosure
272 | if (!is_numeric($length) || $length < 0)
273 | die('The length parameter must be an integer and greater or equals to zero.');
274 |
275 | // Regex used from RFC 4287, page 41
276 | if (!is_string($type) || preg_match('/.+\/.+/', $type) != 1)
277 | die('type parameter must be a string and a MIME type.');
278 |
279 | $attributes = array('length' => $length, 'type' => $type);
280 |
281 | if ($this->version == Feed::RSS2) {
282 | $attributes['url'] = $url;
283 | $this->addElement('enclosure', '', $attributes, FALSE, $multiple);
284 | } else {
285 | $attributes['href'] = $url;
286 | $attributes['rel'] = 'enclosure';
287 | $this->addElement('atom:link', '', $attributes, FALSE, $multiple);
288 | }
289 |
290 | return $this;
291 | }
292 |
293 | /**
294 | * Alias of addEnclosure, for backward compatibility. Using only this
295 | * method ensures that the 'enclosure' element will be present only once.
296 | *
297 | * @access public
298 | * @param string The URL of the media.
299 | * @param integer The length of the media.
300 | * @param string The MIME type attribute of the media.
301 | * @return self
302 | * @link https://tools.ietf.org/html/rfc4288
303 | * @deprecated Use the addEnclosure method instead.
304 | *
305 | **/
306 | public function setEnclosure($url, $length, $type)
307 | {
308 | return $this->addEnclosure($url, $length, $type, false);
309 | }
310 |
311 | /**
312 | * Set the 'author' element of feed item.
313 | * Not supported in RSS 1.0 feeds.
314 | *
315 | * @access public
316 | * @param string The author of this item
317 | * @param string Optional email address of the author
318 | * @param string Optional URI related to the author
319 | * @return self
320 | */
321 | public function setAuthor($author, $email = null, $uri = null)
322 | {
323 | switch ($this->version) {
324 | case Feed::RSS1: die('The author element is not supported in RSS1 feeds.');
325 | break;
326 | case Feed::RSS2:
327 | if ($email != null)
328 | $author = $email . ' (' . $author . ')';
329 |
330 | $this->addElement('author', $author);
331 | break;
332 | case Feed::ATOM:
333 | $elements = array('name' => $author);
334 |
335 | // Regex from RFC 4287 page 41
336 | if ($email != null && preg_match('/.+@.+/', $email) == 1)
337 | $elements['email'] = $email;
338 |
339 | if ($uri != null)
340 | $elements['uri'] = $uri;
341 |
342 | $this->addElement('author', $elements);
343 | break;
344 | }
345 |
346 | return $this;
347 | }
348 |
349 | /**
350 | * Set the unique identifier of the feed item
351 | *
352 | * On ATOM feeds, the identifier must begin with an valid URI scheme.
353 | *
354 | * @access public
355 | * @param string The unique identifier of this item
356 | * @param boolean The value of the 'isPermaLink' attribute in RSS 2 feeds.
357 | * @return self
358 | */
359 | public function setId($id, $permaLink = false)
360 | {
361 | if ($this->version == Feed::RSS2) {
362 | if (!is_bool($permaLink))
363 | die('The permaLink parameter must be boolean.');
364 |
365 | $permaLink = $permaLink ? 'true' : 'false';
366 |
367 | $this->addElement('guid', $id, array('isPermaLink' => $permaLink));
368 | } elseif ($this->version == Feed::ATOM) {
369 | // Check if the given ID is an valid URI scheme (see RFC 4287 4.2.6)
370 | // The list of valid schemes was generated from http://www.iana.org/assignments/uri-schemes
371 | // by using only permanent or historical schemes.
372 | $validSchemes = array('aaa', 'aaas', 'about', 'acap', 'acct', 'afs', 'cap', 'cid', 'coap', 'coaps', 'crid', 'data', 'dav', 'dict', 'dns', 'dtn', 'dvb', 'example', 'fax', 'file', 'filesystem', 'ftp', 'geo', 'go', 'gopher', 'h323', 'ham', 'http', 'https', 'iax', 'icap', 'icon', 'im', 'imap', 'info', 'ipn', 'ipp', 'ipps', 'iris', 'iris.beep', 'iris.lwz', 'iris.xpc', 'iris.xpcs', 'jabber', 'jms', 'ldap', 'mailserver', 'mailto', 'mid', 'modem', 'msrp', 'msrps', 'mtqp', 'mupdate', 'news', 'nfs', 'ni', 'nih', 'nntp', 'opaquelocktoken', 'pack', 'pkcs11', 'pop', 'pres', 'prospero', 'reload', 'rsync', 'rtsp', 'rtsps', 'rtspu', 'service', 'session', 'shttp', 'sieve', 'sip', 'sips', 'sms', 'snews', 'snmp', 'soap.beep', 'soap.beeps', 'stun', 'stuns', 'tag', 'tel', 'telnet', 'tftp', 'thismessage', 'tip', 'tn3270', 'turn', 'turns', 'tv', 'urn', 'vemmi', 'videotex', 'wais', 'ws', 'wss', 'xcon', 'xcon-userid', 'xmlrpc.beep', 'xmlrpc.beeps', 'xmpp', 'z39.50', 'z39.50r', 'z39.50s');
373 | $found = FALSE;
374 | $checkId = strtolower($id);
375 |
376 | foreach($validSchemes as $scheme)
377 | if (strrpos($checkId, $scheme . ':', -strlen($checkId)) !== FALSE)
378 | {
379 | $found = TRUE;
380 | break;
381 | }
382 |
383 | if (!$found)
384 | die("The ID must begin with an IANA-registered URI scheme.");
385 |
386 | $this->addElement('id', $id, NULL, TRUE);
387 | } else
388 | die('A unique ID is not supported in RSS1 feeds.');
389 |
390 | return $this;
391 | }
392 |
393 | } // end of class Item
394 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Generate RSS 1.0, RSS 2.0 or ATOM formatted feeds
2 | ====================================================
3 |
4 | This package can be used to generate feeds in either RSS 1.0, RSS 2.0 or ATOM
5 | formats.
6 |
7 | Applications can create feed writer object, several feed item objects, set
8 | several types of properties of either feeds and feed items, and add items to
9 | the feed.
10 |
11 | Once a feed is fully composed with its items, the feed writer class can generate
12 | the necessary XML structure to describe the feed in the RSS or ATOM formats.
13 | The feed is generated as part of the current feed output.
14 |
15 |
16 | Requirements
17 | ---------------
18 |
19 | PHP >= 5.3
20 |
21 | If you don't have 5.3 available on your system, there's a version supporting
22 | PHP >= 5.0 in the "legacy-php-5.0" branch.
23 |
24 |
25 | Documentation
26 | --------------
27 |
28 | The documentation can be found in the "gh-pages" branch or on GitHub Pages:
29 | http://mibe.github.io/FeedWriter/
30 |
31 | Check **/examples** directory for usages examples.
32 |
33 |
34 | Authors
35 | ----------
36 | (in chronological order)
37 |
38 | - [Anis uddin Ahmad](https://github.com/ajaxray)
39 | - [Michael Bemmerl](https://github.com/mibe)
40 | - Phil Freo
41 | - Paul Ferrett
42 | - Brennen Bearnes
43 | - Michael Robinson
44 | - Baptiste Fontaine
45 | - Kristián Valentín
46 | - Brandtley McMinn
47 | - Julian Bogdani
48 |
--------------------------------------------------------------------------------
/RSS1.php:
--------------------------------------------------------------------------------
1 |
6 | *
7 | * This file is part of the "Universal Feed Writer" project.
8 | *
9 | * This program is free software: you can redistribute it and/or modify
10 | * it under the terms of the GNU General Public License as published by
11 | * the Free Software Foundation, either version 3 of the License, or
12 | * (at your option) any later version.
13 | *
14 | * This program is distributed in the hope that it will be useful,
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | * GNU General Public License for more details.
18 | *
19 | * You should have received a copy of the GNU General Public License
20 | * along with this program. If not, see .
21 | */
22 |
23 | /**
24 | * Wrapper for creating RSS1 feeds
25 | *
26 | * @package UniversalFeedWriter
27 | */
28 | class RSS1 extends Feed
29 | {
30 | /**
31 | * {@inheritdoc}
32 | */
33 | public function __construct()
34 | {
35 | parent::__construct(Feed::RSS1);
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/RSS2.php:
--------------------------------------------------------------------------------
1 |
6 | *
7 | * This file is part of the "Universal Feed Writer" project.
8 | *
9 | * This program is free software: you can redistribute it and/or modify
10 | * it under the terms of the GNU General Public License as published by
11 | * the Free Software Foundation, either version 3 of the License, or
12 | * (at your option) any later version.
13 | *
14 | * This program is distributed in the hope that it will be useful,
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | * GNU General Public License for more details.
18 | *
19 | * You should have received a copy of the GNU General Public License
20 | * along with this program. If not, see .
21 | */
22 |
23 | /**
24 | * Wrapper for creating RSS2 feeds
25 | *
26 | * @package UniversalFeedWriter
27 | */
28 | class RSS2 extends Feed
29 | {
30 | /**
31 | * {@inheritdoc}
32 | */
33 | public function __construct()
34 | {
35 | parent::__construct(Feed::RSS2);
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "mibe/feedwriter",
3 | "description": "Generate feeds in either RSS 1.0, RSS 2.0 or ATOM formats",
4 | "keywords": ["RSS", "RSS 1.0", "RSS 2.0", "RSS2", "ATOM", "feed"],
5 | "homepage": "https://github.com/mibe/FeedWriter",
6 | "type": "library",
7 | "license": "GPL-3.0",
8 | "authors": [
9 | {
10 | "name": "Anis uddin Ahmad",
11 | "email": "anis.programmer@gmail.com"
12 | },
13 | {
14 | "name": "Michael Bemmerl",
15 | "email": "mail@mx-server.de"
16 | },
17 | {
18 | "name": "Phil Freo"
19 | },
20 | {
21 | "name": "Paul Ferrett"
22 | },
23 | {
24 | "name": "Brennen Bearnes"
25 | },
26 | {
27 | "name": "Michael Robinson",
28 | "email": "mike@pagesofinterest.net"
29 | },
30 | {
31 | "name": "Baptiste Fontaine"
32 | },
33 | {
34 | "name": "Kristián Valentín"
35 | },
36 | {
37 | "name": "Brandtley McMinn"
38 | },
39 | {
40 | "name": "Julian Bogdani"
41 | }
42 | ],
43 | "minimum-stability": "dev",
44 | "autoload": {
45 | "classmap": [
46 | "ATOM.php",
47 | "Feed.php",
48 | "Item.php",
49 | "RSS1.php",
50 | "RSS2.php"
51 | ]
52 | },
53 | "require" : {
54 | "php": ">=5.3.0"
55 | },
56 | "extra": {
57 | "branch-alias": {
58 | "dev-master": "1.0.x-dev"
59 | }
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/examples/example_atom.php:
--------------------------------------------------------------------------------
1 |
15 | *
16 | * This file is part of the "Universal Feed Writer" project.
17 | *
18 | * This program is free software: you can redistribute it and/or modify
19 | * it under the terms of the GNU General Public License as published by
20 | * the Free Software Foundation, either version 3 of the License, or
21 | * (at your option) any later version.
22 | *
23 | * This program is distributed in the hope that it will be useful,
24 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 | * GNU General Public License for more details.
27 | *
28 | * You should have received a copy of the GNU General Public License
29 | * along with this program. If not, see .
30 | */
31 |
32 | // IMPORTANT : No need to add id for feed or channel. It will be automatically created from link.
33 |
34 | //Creating an instance of ATOM class.
35 | $TestFeed = new ATOM;
36 |
37 | //Setting the channel elements
38 | //Use wrapper functions for common elements
39 | $TestFeed->setTitle('Testing the RSS writer class');
40 | $TestFeed->setLink('http://www.ajaxray.com/rss2/channel/about');
41 | $TestFeed->setDate(new DateTime());
42 |
43 | //For other channel elements, use setChannelElement() function
44 | $TestFeed->setChannelElement('author', array('name'=>'Anis uddin Ahmad'));
45 |
46 | //You can add additional link elements, e.g. to a PubSubHubbub server with custom relations.
47 | $TestFeed->setSelfLink('http://example.com/myfeed');
48 | $TestFeed->setAtomLink('http://pubsubhubbub.appspot.com', 'hub');
49 |
50 | //Adding a feed. Generally this portion will be in a loop and add all feeds.
51 |
52 | //Create an empty Item
53 | $newItem = $TestFeed->createNewItem();
54 |
55 | //Add elements to the feed item
56 | //Use wrapper functions to add common feed elements
57 | $newItem->setTitle('The first feed');
58 | $newItem->setLink('http://www.yahoo.com');
59 | $newItem->setDate(time());
60 | $newItem->setAuthor('Anis uddin Ahmad', 'anis@example.invalid');
61 | $newItem->setEnclosure('http://upload.wikimedia.org/wikipedia/commons/4/49/En-us-hello-1.ogg', 11779, 'audio/ogg');
62 |
63 | //Internally changed to "summary" tag for ATOM feed
64 | $newItem->setDescription('This is a test of adding CDATA encoded description by the php Universal Feed Writer class');
65 | $newItem->setContent('hi.
This is the content for the entry.
');
66 |
67 | //Now add the feed item
68 | $TestFeed->addItem($newItem);
69 |
70 | //OK. Everything is done. Now generate the feed.
71 | $TestFeed->printFeed();
72 |
--------------------------------------------------------------------------------
/examples/example_minimum.php:
--------------------------------------------------------------------------------
1 |
15 | *
16 | * This file is part of the "Universal Feed Writer" project.
17 | *
18 | * This program is free software: you can redistribute it and/or modify
19 | * it under the terms of the GNU General Public License as published by
20 | * the Free Software Foundation, either version 3 of the License, or
21 | * (at your option) any later version.
22 | *
23 | * This program is distributed in the hope that it will be useful,
24 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 | * GNU General Public License for more details.
27 | *
28 | * You should have received a copy of the GNU General Public License
29 | * along with this program. If not, see .
30 | */
31 |
32 | //Creating an instance of RSS2 class.
33 | $TestFeed = new RSS2;
34 |
35 | //Setting the channel elements
36 | //Use wrapper functions for common channel elements
37 | $TestFeed->setTitle('Testing & Checking the RSS writer class');
38 | $TestFeed->setLink('http://www.ajaxray.com');
39 | $TestFeed->setDescription('This is a test of creating a RSS 2.0 feed Universal Feed Writer');
40 |
41 | //Image title and link must match with the 'title' and 'link' channel elements for valid RSS 2.0
42 | $TestFeed->setImage('Testing & Checking the RSS writer class','http://www.ajaxray.com','http://www.rightbrainsolution.com/_resources/img/logo.png');
43 |
44 | //Let's add some feed items: Create two empty Item instances
45 | $itemOne = $TestFeed->createNewItem();
46 | $itemTwo = $TestFeed->createNewItem();
47 |
48 | //Add item details
49 | $itemOne->setTitle('The title of the first entry.');
50 | $itemOne->setLink('http://www.google.de');
51 | $itemOne->setDate(time());
52 | $itemOne->setDescription('And here\'s the description of the entry.');
53 |
54 | $itemTwo->setTitle('Lorem ipsum');
55 | $itemTwo->setLink('http://www.example.com');
56 | $itemTwo->setDate(1234567890);
57 | $itemTwo->setDescription('Lorem ipsum dolor sit amet, consectetur, adipisci velit');
58 |
59 | //Now add the feed item
60 | $TestFeed->addItem($itemOne);
61 | $TestFeed->addItem($itemTwo);
62 |
63 | //OK. Everything is done. Now generate the feed.
64 | $TestFeed->printFeed();
65 |
--------------------------------------------------------------------------------
/examples/example_rss1.php:
--------------------------------------------------------------------------------
1 |
15 | *
16 | * This file is part of the "Universal Feed Writer" project.
17 | *
18 | * This program is free software: you can redistribute it and/or modify
19 | * it under the terms of the GNU General Public License as published by
20 | * the Free Software Foundation, either version 3 of the License, or
21 | * (at your option) any later version.
22 | *
23 | * This program is distributed in the hope that it will be useful,
24 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 | * GNU General Public License for more details.
27 | *
28 | * You should have received a copy of the GNU General Public License
29 | * along with this program. If not, see .
30 | */
31 |
32 | //Creating an instance of RSS1 class.
33 | $TestFeed = new RSS1;
34 |
35 | //Setting the channel elements
36 | //Use wrapper functions for common elements
37 | //For other optional channel elements, use setChannelElement() function
38 | $TestFeed->setTitle('Testing the RSS writer class');
39 | $TestFeed->setLink('http://www.ajaxray.com/rss2/channel/about');
40 | $TestFeed->setDescription('This is test of creating a RSS 1.0 feed by Universal Feed Writer');
41 |
42 | //It's important for RSS 1.0
43 | $TestFeed->setChannelAbout('http://www.ajaxray.com/rss2/channel/about');
44 |
45 | //Adding a feed. Generally this portion will be in a loop and add all feeds.
46 |
47 | //Create an empty FeedItem
48 | $newItem = $TestFeed->createNewItem();
49 |
50 | //Add elements to the feed item
51 | //Use wrapper functions to add common feed elements
52 | $newItem->setTitle('The first feed');
53 | $newItem->setLink('http://www.yahoo.com');
54 | //The parameter is a timestamp for setDate() function
55 | $newItem->setDate(time());
56 | $newItem->setDescription('This is test of adding CDATA encoded description by the php Universal Feed Writer class');
57 | //Use core addElement() function for other supported optional elements
58 | $newItem->addElement('dc:subject', 'Nothing but test');
59 |
60 | //Now add the feed item
61 | $TestFeed->addItem($newItem);
62 |
63 | //Adding multiple elements from array
64 | //Elements which have an attribute cannot be added by this way
65 | $newItem = $TestFeed->createNewItem();
66 | $newItem->addElementArray(array('title'=>'The 2nd feed', 'link'=>'http://www.google.com', 'description'=>'This is a test of the FeedWriter class'));
67 | $TestFeed->addItem($newItem);
68 |
69 | //OK. Everything is done. Now generate the feed.
70 | $TestFeed->printFeed();
71 |
--------------------------------------------------------------------------------
/examples/example_rss2.php:
--------------------------------------------------------------------------------
1 |
15 | * Copyright (C) 2013 Michael Bemmerl
16 | *
17 | * This file is part of the "Universal Feed Writer" project.
18 | *
19 | * This program is free software: you can redistribute it and/or modify
20 | * it under the terms of the GNU General Public License as published by
21 | * the Free Software Foundation, either version 3 of the License, or
22 | * (at your option) any later version.
23 | *
24 | * This program is distributed in the hope that it will be useful,
25 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 | * GNU General Public License for more details.
28 | *
29 | * You should have received a copy of the GNU General Public License
30 | * along with this program. If not, see .
31 | */
32 |
33 | // Creating an instance of RSS2 class.
34 | $TestFeed = new RSS2;
35 |
36 | // Setting some basic channel elements. These three elements are mandatory.
37 | $TestFeed->setTitle('Testing & Checking the Feed Writer project');
38 | $TestFeed->setLink('https://github.com/mibe/FeedWriter');
39 | $TestFeed->setDescription('This is just an example how to use the Feed Writer project in your code.');
40 |
41 | // Image title and link must match with the 'title' and 'link' channel elements for RSS 2.0,
42 | // which were set above.
43 | $TestFeed->setImage('Testing & Checking the Feed Writer project', 'https://github.com/mibe/FeedWriter', 'https://upload.wikimedia.org/wikipedia/commons/thumb/d/d9/Rss-feed.svg/256px-Rss-feed.svg.png');
44 |
45 | // Use core setChannelElement() function for other optional channel elements.
46 | // See http://www.rssboard.org/rss-specification#optionalChannelElements
47 | // for other optional channel elements. Here the language code for American English and
48 | $TestFeed->setChannelElement('language', 'en-US');
49 |
50 | // The date when this feed was lastly updated. The publication date is also set.
51 | $TestFeed->setDate(date(DATE_RSS, time()));
52 | $TestFeed->setChannelElement('pubDate', date(\DATE_RSS, strtotime('2013-04-06')));
53 |
54 | // By using arrays as channelElement values, can be set element like this
55 | //
56 | // Saturday
57 | // Sunday
58 | //
59 | // Thanks to - Peter Fargaš
60 | $TestFeed->setChannelElement("skipDays", array('day'=>['Saturday','Sunday']));
61 |
62 | // You can add additional link elements, e.g. to a PubSubHubbub server with custom relations.
63 | // It's recommended to provide a backlink to the feed URL.
64 | $TestFeed->setSelfLink('http://example.com/myfeed');
65 | $TestFeed->setAtomLink('http://pubsubhubbub.appspot.com', 'hub');
66 |
67 | // You can add more XML namespaces for more custom channel elements which are not defined
68 | // in the RSS 2 specification. Here the 'creativeCommons' element is used. There are much more
69 | // available. Have a look at this list: http://feedvalidator.org/docs/howto/declare_namespaces.html
70 | $TestFeed->addNamespace('creativeCommons', 'http://backend.userland.com/creativeCommonsRssModule');
71 | $TestFeed->setChannelElement('creativeCommons:license', 'http://www.creativecommons.org/licenses/by/1.0');
72 |
73 | // If you want you can also add a line to publicly announce that you used
74 | // this fine piece of software to generate the feed. ;-)
75 | $TestFeed->addGenerator();
76 |
77 | // Here we are done setting up the feed. What's next is adding some feed items.
78 |
79 | // Create a new feed item.
80 | $newItem = $TestFeed->createNewItem();
81 |
82 | // Add basic elements to the feed item
83 | // These are again mandatory for a valid feed.
84 | $newItem->setTitle('Hello World!');
85 | $newItem->setLink('http://www.example.com');
86 | $newItem->setDescription('This is a test of adding a description by the Feed Writer classes. It\'s automatically CDATA encoded.');
87 |
88 | // The following method calls add some optional elements to the feed item.
89 |
90 | // Let's set the publication date of this item. You could also use a UNIX timestamp or
91 | // an instance of PHP's DateTime class.
92 | $newItem->setDate('2013-04-07 00:50:30');
93 |
94 | // You can also attach a media object to a feed item. You just need the URL, the byte length
95 | // and the MIME type of the media. Here's a quirk: The RSS2 spec says "The url must be an http url.".
96 | // Other schemes like ftp, https, etc. produce an error in feed validators.
97 | $newItem->setEnclosure('http://upload.wikimedia.org/wikipedia/commons/4/49/En-us-hello-1.ogg', 11779, 'audio/ogg');
98 |
99 | // If you want you can set the name (and email address) of the author of this feed item.
100 | $newItem->setAuthor('Anis uddin Ahmad', 'admin@ajaxray.com');
101 |
102 | // You can set a globally unique identifier. This can be a URL or any other string.
103 | // If you set permaLink to true, the identifier must be an URL. The default of the
104 | // permaLink parameter is false.
105 | $newItem->setId('http://example.com/URL/to/article', true);
106 |
107 | // Use the addElement() method for other optional elements.
108 | // This here will add the 'source' element. The second parameter is the value of the element
109 | // and the third is an array containing the element attributes.
110 | $newItem->addElement('source', 'Mike\'s page', array('url' => 'http://www.example.com'));
111 |
112 | // Now add the feed item to the main feed.
113 | $TestFeed->addItem($newItem);
114 |
115 | // Another method to add feeds items is by using an array which contains key-value pairs
116 | // of every item element. Elements which have attributes cannot be added by this way.
117 | $newItem = $TestFeed->createNewItem();
118 | $newItem->addElementArray(array('title'=> 'The 2nd item', 'link' => 'http://www.google.com', 'description' => 'Just another test.'));
119 | $TestFeed->addItem($newItem);
120 |
121 | // OK. Everything is done. Now generate the feed.
122 | // Then do anything (e,g cache, save, attach, print) you want with the feed in $myFeed.
123 | $myFeed = $TestFeed->generateFeed();
124 |
125 | // If you want to send the feed directly to the browser, use the printFeed() method.
126 | $TestFeed->printFeed();
--------------------------------------------------------------------------------