Skip to content

Basic Feed

Rumen Damyanov edited this page Jul 29, 2025 · 6 revisions

Basic Feed Example

This example shows the simplest way to create RSS and Atom feeds using the php-feed package.

Quick Start

The fastest way to create a feed is using the factory method:

<?php
require 'vendor/autoload.php';

use Rumenx\Feed\FeedFactory;

// Create a new feed instance
$feed = FeedFactory::create();

// Set feed metadata
$feed->setTitle('My Blog Feed');
$feed->setDescription('Latest posts from my blog');
$feed->setLink('https://example.com');
$feed->setLanguage('en');

// Add items to the feed
$feed->addItem([
    'title' => 'Welcome to My Blog',
    'author' => 'John Doe',
    'link' => 'https://example.com/welcome',
    'pubdate' => '2025-01-15T10:00:00Z',
    'description' => 'This is my first blog post. Welcome to my website!'
]);

$feed->addItem([
    'title' => 'Learning PHP Feeds',
    'author' => 'John Doe', 
    'link' => 'https://example.com/php-feeds',
    'pubdate' => '2025-01-10T14:30:00Z',
    'description' => 'A guide to creating RSS and Atom feeds in PHP.'
]);

// Generate RSS feed
header('Content-Type: application/rss+xml; charset=utf-8');
echo $feed->render('rss');

RSS vs Atom

You can generate both RSS and Atom feeds with the same data:

// Generate RSS 2.0 feed
$rssXml = $feed->render('rss');

// Generate Atom 1.0 feed  
$atomXml = $feed->render('atom');

Adding Multiple Items at Once

For better performance when adding many items:

$items = [
    [
        'title' => 'Post 1',
        'author' => 'Author 1',
        'link' => 'https://example.com/post-1',
        'pubdate' => '2025-01-01T12:00:00Z',
        'description' => 'First post content'
    ],
    [
        'title' => 'Post 2', 
        'author' => 'Author 2',
        'link' => 'https://example.com/post-2',
        'pubdate' => '2025-01-02T12:00:00Z',
        'description' => 'Second post content'
    ]
];

$feed->addItems($items);

Item Properties

Each feed item supports these properties:

  • title (required) - The item title
  • link (required) - The item URL
  • description (required) - The item description/content
  • author - The item author
  • pubdate - Publication date (ISO 8601 format recommended)
  • guid - Unique identifier (defaults to link if not provided)
  • category - Categories (array or single string)
  • enclosure - Media enclosure (for podcasts, videos, etc.)

Feed Metadata Methods

Configure your feed with these methods:

$feed->setTitle('Feed Title');              // Required
$feed->setDescription('Feed Description');  // Required  
$feed->setLink('https://example.com');      // Required
$feed->setLanguage('en');                   // Optional, defaults to 'en'
$feed->setDateFormat('datetime');           // Optional: 'datetime', 'timestamp', or 'carbon'

Date Formats

The package supports multiple date formats:

// ISO 8601 (recommended)
'pubdate' => '2025-01-15T10:00:00Z'

// RFC 2822 
'pubdate' => 'Mon, 15 Jan 2025 10:00:00 +0000'

// Unix timestamp
'pubdate' => 1736942400

// DateTime object
'pubdate' => new DateTime('2025-01-15 10:00:00')

Complete Example

Here's a complete example that creates a feed from a database:

<?php
require 'vendor/autoload.php';

use Rumenx\Feed\FeedFactory;
use PDO;

// Database connection
$pdo = new PDO('mysql:host=localhost;dbname=blog', $username, $password);

// Create feed
$feed = FeedFactory::create();
$feed->setTitle('My Blog - Latest Posts');
$feed->setDescription('The latest posts from my blog');
$feed->setLink('https://myblog.com');
$feed->setLanguage('en');

// Get latest posts from database
$stmt = $pdo->prepare('
    SELECT title, author, slug, created_at, excerpt 
    FROM posts 
    WHERE published = 1 
    ORDER BY created_at DESC 
    LIMIT 20
');
$stmt->execute();
$posts = $stmt->fetchAll(PDO::FETCH_ASSOC);

// Add posts to feed
foreach ($posts as $post) {
    $feed->addItem([
        'title' => $post['title'],
        'author' => $post['author'],
        'link' => 'https://myblog.com/posts/' . $post['slug'],
        'pubdate' => $post['created_at'],
        'description' => $post['excerpt']
    ]);
}

// Output RSS feed
header('Content-Type: application/rss+xml; charset=utf-8');
echo $feed->render('rss');

Next Steps

Clone this wiki locally