Создание RSS ленты на своем сайте | Свой RSS поток | Как Создать RSS поток? | Класс для создания RSS потока.

[h1]Создание RSS ленты на своем сайте при помощи DOMElement[/h1] Этот класс, как следует из названия, создает канал. RSS-класс расширяет класс DOMDocument, которая встроена в PHP, который позволит прямой доступ ко всем функциональным расширением DOMElement.
(перевод) Каждый RSS пункта определяется как массив и передаются additem метода. Пункты включать изображения и другие элементы, которые подвида. AddItem метод возвращает экземпляр класса, с тем чтобы метод цепочки, как показано на примере использования. RSS-класс поддерживает все элементы из RSS2 Спецификация включает в себя и даже изображения, skipDays и skipHours, которые содержат вложенные элементы. Под элементы просто передается как массив значений, а затем добавить их в структуру XML. Поддерживает имена элемента находятся в выключатель (см. phpswitch.com), чтобы обеспечить действительно только элементы RSS предоставляются, а также делегатов, что должно произойти, чтобы каждый действительный элемент объявления. Неверный элементы quitly использоваться в качестве не может быть и исключения, при использовании __toString. __toString Метод оказывается покончить с необходимостью отдельного метода getRSS поэтому объект echo'ed непосредственно в этом примере. Тем не менее, рекомендуется, чтобы при использовании этого класса, то вывод будет записан в файл и файл, используемый в качестве точки доступа для RSS-каналы. Это позволит вам сэкономить много накладных расходов, особенно если пункты создаются в базе данных результирующего набора.
 Code: php
<?php

/**
 *
 * A class to create RSS feeds using DOM
 *
 * @Author Kevin Waterson
 *
 * @copyright 2009
 *
 * @author Kevin Waterson
 *
 * @license BSD
 *
 */
class rss extends DomDocument
{
    /**
     * @ the RSS channel
     */
    private $channel;

    /**
     *
     * @Constructor, duh! Set up the DOM environment
     *
     * @access public
     *
     * @param string $title The site title
     *
     * @param string $link The link to the site
     *
     * @param string $description The site description
     *
     */
    public function __construct($title, $link, $description)
    {
        /*** call the parent constructor ***/
        parent::__construct();

        /*** format the created XML ***/
        $this->formatOutput = true;

        /*** craete the root element ***/
        $root = $this->appendChild($this->createElement('rss'));

        /*** set to rss2 ***/
        $root->setAttribute('version', '2.0');

        /*** set the channel node **/
        $this->channel = $root->appendChild($this->createElement('channel'));

        /*** set the title link and description elements ***/
        $this->channel->appendChild($this->createElement('title', $title));
        $this->channel->appendChild($this->createElement('link', $link));
        $this->channel->appendChild($this->createElement('description', $description));
    }


    /**
     *
     * @Add Items to the RSS Feed
     *
     * @access public
     *
     * @param array $items
     *
     * @return object Instance of self for method chaining
     *
     */
    public function addItem($items)
    {
        /*** create an item ***/
        $item = $this->createElement('item');
        foreach($items as $element=>$value)
        {
            switch($element)
            {
                /*** create sub elements here ***/
                case 'image':
                case 'skipHour':
                case 'skipDay':
                $im = $this->createElement($element);
                $this->channel->appendChild($im);
                foreach( $value as $sub_element=>$sub_value )
                {
                    $sub = $this->createElement($sub_element, $sub_value);
                    $im->appendChild( $sub );
                }
                break;

                case 'title':
                case 'pubDate':
                case 'link':
                case 'description':
                case 'copyright':
                case 'managingEditor':
                case 'webMaster':
                case 'lastbuildDate':
                case 'category':
                case 'generator':
                case 'docs':
                case 'language':
                case 'cloud':
                case 'ttl':
                case 'rating':
                case 'textInput':
                case 'source':
                $new = $item->appendChild($this->createElement($element, $value));
                break;
            }
        }
        /*** append the item to the channel ***/
        $this->channel->appendChild($item);

        /*** allow chaining ***/
        return $this;
    }

    /***
     *
     * @create the XML
     *
     * @access public
     *
     * @return string The XML string
     *
     */
    public function __toString()
    {
        return $this->saveXML();
    }
}
?>
Пример использования:
 Code:
<?php

/*** the first article item ***/
$item1 = array(
    'title'=>'Zend Framework Example Site', 
    'link'=>'http://www.phpro.org/articles/Zend-Framework-Example-Site.html', 
    'description'=>'This example site hopes to introduce the newcomers to Zend Framework in a friendly way, by providing a simple modular site layout and can have the newcomer up and running in minutes.', 
    'pubDate'=>date(DATE_RSS),
    'image'=>array('link'=>'http://phpro.org', 'url'=>'http://phpro.org/images/spork.jpg', 'title'=>'SPORK'),
    'language'=>'en');

/*** second article item ***/
$item2 = array(
    'title'=>'Xajax-In An Object Oriented Environment',
    'link'=>'http://www.phpro.org/tutorials/Xajax-In-An-Object-Oriented-Environment.html',
    'description'=>'This tutorial takes the next step in development and shows, by way of example, how xajax can be utilized in an Object Oriented environment',
    'pubDate'=>date(DATE_RSS),
    'language'=>'en');

/*** third article item ***/
$item3 = array(
    'title'=>'Introduction to SPL DirectoryIterator',
    'link'=>'http://www.phpro.org/tutorials/Introduction-to-SPL-DirectoryIterator.html',
    'description'=>'The DirectoryIterator is one of the more oft used of the Iterator classes and this tutorial helps to expose the user to developing in a standardised and Object Oriented approach',
    'pubDate'=>date(DATE_RSS),
    'language'=>'en');

/*** a new RSS instance, pass values to the constructor ***/
$rss = new rss('PHPRO.ORG', 'http://phpro.org', 'PHP Articles Tutorials Examples Classes');

/*** add the items from above ***/
$rss    ->addItem($item1)
    ->addItem($item2)
    ->addItem($item3);

/*** show the RSS Feed ***/
echo $rss;

?>
Результат:
 Code: xml
<?xml version="1.0"?>
<rss version="2.0">
  <channel>
    <title>PHPRO.ORG</title>
    <link>http://phpro.org</link>
    <description>PHP Articles Tutorials Examples Classes</description>
    <image>
      <link>http://phpro.org</link>

      <url>http://phpro.org/images/spork.jpg</url>
      <title>SPORK</title>
    </image>
    <item>
      <title>Zend Framework Example Site</title>
      <link>http://www.phpro.org/articles/Zend-Framework-Example-Site.html</link>
      <description>This example site hopes to introduce the newcomers to Zend Framework in a friendly way, by providing a simple modular site layout and can have the newcomer up and running in minutes.</description>

      <pubDate>Thu, 01 Jan 2009 08:11:20 +1100</pubDate>
      <language>en</language>
    </item>
    <item>
      <title>Xajax-In An Object Oriented Environment</title>
      <link>http://www.phpro.org/tutorials/Xajax-In-An-Object-Oriented-Environment.html</link>
      <description>This tutorial takes the next step in development and shows, by way of example, how xajax can be utilized in an Object Oriented environment</description>

      <pubDate>Thu, 01 Jan 2009 08:11:20 +1100</pubDate>
      <language>en</language>
    </item>
    <item>
      <title>Introduction to SPL DirectoryIterator</title>
      <link>http://www.phpro.org/tutorials/Introduction-to-SPL-DirectoryIterator.html</link>
      <description>The DirectoryIterator is one of the more oft used of the Iterator classes and this tutorial helps to expose the user to developing in a standardised and Object Oriented approach</description>

      <pubDate>Thu, 01 Jan 2009 08:11:20 +1100</pubDate>
      <language>en</language>
    </item>
  </channel>
</rss>
Немного поколдуете и адаптируете его под свою БД и свой сайт. Я пользуюсь именно этим классом и доволен. Единственно, что его неудобно пользовать в Win1251, а мне неудобно хранить данные в UTF8, т.к. она занимают значительно больше места.
Защитное изображение для HTML-формы
unix timestump to Last-Modified gmdate Ковертация временого штампа (даты) в дату по гринвичу, для Last-Modified
Заменяем getimagesize на CURL. И get_headers на CURL
Хостинг не оплачен.