PHP Universal Feed Generator
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
5.7 KiB

  1. <?php
  2. // You should use an autoloader instead of including the files directly.
  3. // This is done here only to make the examples work out of the box.
  4. include '../Item.php';
  5. include '../Feed.php';
  6. include '../RSS2.php';
  7. date_default_timezone_set('UTC');
  8. use \FeedWriter\RSS2;
  9. /**
  10. * Copyright (C) 2008 Anis uddin Ahmad <anisniit@gmail.com>
  11. * Copyright (C) 2013 Michael Bemmerl <mail@mx-server.de>
  12. *
  13. * This file is part of the "Universal Feed Writer" project.
  14. *
  15. * This program is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation, either version 3 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. */
  28. // Creating an instance of RSS2 class.
  29. $TestFeed = new RSS2;
  30. // Setting some basic channel elements. These three elements are mandatory.
  31. $TestFeed->setTitle('Testing & Checking the Feed Writer project');
  32. $TestFeed->setLink('https://github.com/mibe/FeedWriter');
  33. $TestFeed->setDescription('This is just an example how to use the Feed Writer project in your code.');
  34. // Image title and link must match with the 'title' and 'link' channel elements for RSS 2.0,
  35. // which were set above.
  36. $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');
  37. // Use core setChannelElement() function for other optional channel elements.
  38. // See http://www.rssboard.org/rss-specification#optionalChannelElements
  39. // for other optional channel elements. Here the language code for American English and
  40. $TestFeed->setChannelElement('language', 'en-US');
  41. // The date when this feed was lastly updated. The publication date is also set.
  42. $TestFeed->setDate(date(DATE_RSS, time()));
  43. $TestFeed->setChannelElement('pubDate', date(\DATE_RSS, strtotime('2013-04-06')));
  44. // You can add additional link elements, e.g. to a PubSubHubbub server with custom relations.
  45. // It's recommended to provide a backlink to the feed URL.
  46. $TestFeed->setSelfLink('http://example.com/myfeed');
  47. $TestFeed->setAtomLink('http://pubsubhubbub.appspot.com', 'hub');
  48. // You can add more XML namespaces for more custom channel elements which are not defined
  49. // in the RSS 2 specification. Here the 'creativeCommons' element is used. There are much more
  50. // available. Have a look at this list: http://feedvalidator.org/docs/howto/declare_namespaces.html
  51. $TestFeed->addNamespace('creativeCommons', 'http://backend.userland.com/creativeCommonsRssModule');
  52. $TestFeed->setChannelElement('creativeCommons:license', 'http://www.creativecommons.org/licenses/by/1.0');
  53. // If you want you can also add a line to publicly announce that you used
  54. // this fine piece of software to generate the feed. ;-)
  55. $TestFeed->addGenerator();
  56. // Here we are done setting up the feed. What's next is adding some feed items.
  57. // Create a new feed item.
  58. $newItem = $TestFeed->createNewItem();
  59. // Add basic elements to the feed item
  60. // These are again mandatory for a valid feed.
  61. $newItem->setTitle('Hello World!');
  62. $newItem->setLink('http://www.example.com');
  63. $newItem->setDescription('This is a test of adding a description by the <b>Feed Writer</b> classes. It\'s automatically CDATA encoded.');
  64. // The following method calls add some optional elements to the feed item.
  65. // Let's set the publication date of this item. You could also use a UNIX timestamp or
  66. // an instance of PHP's DateTime class.
  67. $newItem->setDate('2013-04-07 00:50:30');
  68. // You can also attach a media object to a feed item. You just need the URL, the byte length
  69. // and the MIME type of the media. Here's a quirk: The RSS2 spec says "The url must be an http url.".
  70. // Other schemes like ftp, https, etc. produce an error in feed validators.
  71. $newItem->setEnclosure('http://upload.wikimedia.org/wikipedia/commons/4/49/En-us-hello-1.ogg', 11779, 'audio/ogg');
  72. // If you want you can set the name (and email address) of the author of this feed item.
  73. $newItem->setAuthor('Anis uddin Ahmad', 'admin@ajaxray.com');
  74. // You can set a globally unique identifier. This can be a URL or any other string.
  75. // If you set permaLink to true, the identifier must be an URL. The default of the
  76. // permaLink parameter is false.
  77. $newItem->setId('http://example.com/URL/to/article', true);
  78. // Use the addElement() method for other optional elements.
  79. // This here will add the 'source' element. The second parameter is the value of the element
  80. // and the third is an array containing the element attributes.
  81. $newItem->addElement('source', 'Mike\'s page', array('url' => 'http://www.example.com'));
  82. // Now add the feed item to the main feed.
  83. $TestFeed->addItem($newItem);
  84. // Another method to add feeds items is by using an array which contains key-value pairs
  85. // of every item element. Elements which have attributes cannot be added by this way.
  86. $newItem = $TestFeed->createNewItem();
  87. $newItem->addElementArray(array('title'=> 'The 2nd item', 'link' => 'http://www.google.com', 'description' => 'Just another test.'));
  88. $TestFeed->addItem($newItem);
  89. // OK. Everything is done. Now generate the feed.
  90. // If you want to send the feed directly to the browser, use the printFeed() method.
  91. $myFeed = $TestFeed->generateFeed();
  92. // Do anything you want with the feed in $myFeed. Why not send it to the browser? ;-)
  93. // You could also save it to a file if you don't want to invoke your script every time.
  94. echo $myFeed;