shevabam / rss-feed-maker
Create RSS feeds easily in PHP
1.0.1
2023-03-28 15:21 UTC
Requires
- php: >=7.4.0
README
Create RSS feeds easily in PHP!
This library allows you to create an XML file representing an RSS feed.
Requirement
- PHP 7.4+
Installation
With Composer, run this command:
composer require shevabam/rss-feed-maker
Usage
First, include the library in your code using the Composer autoloader and then create a Feed object.
require 'vendor/autoload.php'; $feed = new \RssFeedMaker\Feed;
Next, configure the feed:
$feed ->setTitle('RSS Feed Title') ->setDescription('Recent articles on my website') ->setLink('https://website.com') ->setCopyright('MyWebsite.com') ->setLanguage('en') ->setImage([ 'title' => 'Image title', 'url' => 'https://website.com/Image.jpg', 'link' => 'https://website.com', ]) ;
Here are the parameters that can be modified for the feed:
The language
code is described here: https://www.rssboard.org/rss-language-codes
Default encoding is: utf-8. You can change it with:
$feed->setEncoding('iso-8859-1');
Then, create the items and inject them into the feed:
$posts = [ [ 'title' => 'Post title #1', 'link' => 'https://website.com/1-post-title', 'published_at' => '2023-03-18 12:00:00', 'description' => 'Blog post about something very important', ], [ 'title' => 'Post title #2', 'link' => 'https://website.com/2-post-title', 'published_at' => '2023-03-11 16:30:00', 'description' => 'Blog post about something very important', ], ]; foreach ($posts as $post) { $item = new \RssFeedMaker\Item; $item ->setTitle($post['title']) ->setLink($post['link']) ->setDescription($post['description']) ->setPubDate($post['published_at']) ; $feed->addItem($item); }
Parameters for item:
For more information about the RSS schema, please see the specifications.
Finally, generate the XML with:
echo $feed->generate();
You can save the RSS feed to a file:
$feed->save('path/to/the/feed.xml');
Full example
require 'vendor/autoload.php'; $feed = new \RssFeedMaker\Feed; $feed ->setTitle('RSS Feed Title') ->setDescription('Recent articles on your website') ->setLink('https://website.com') ->setCopyright('MyWebsite.com') ->setImage([ 'title' => 'Image title', 'url' => 'https://website.com/Image.jpg', 'link' => 'https://website.com', ]) ; $posts = [ [ 'title' => 'Post title #1', 'link' => 'https://website.com/1-post-title', 'published_at' => '2023-03-14 12:00:00', 'author' => 'John Doe', 'description' => 'Blog post about something very important', ], [ 'title' => 'Post title #2', 'link' => 'https://website.com/2-post-title', 'published_at' => '2023-03-08 16:30:00', 'author' => 'Jane Doe', 'description' => 'Blog post number two', ], [ 'title' => 'Post title #3', 'link' => 'https://website.com/3-post-title', 'published_at' => '2023-03-01 08:45:00', 'enclosure' => [ 'url' => 'https://website.com/podcasts/example.mp3', 'length' => 12345, 'type' => 'audio/mpeg', ], ], ]; foreach ($posts as $post) { $item = new \RssFeedMaker\Item; $item->setTitle($post['title']); $item->setLink($post['link']); $item->setDescription(isset($post['description']) ? $post['description'] : ''); $item->setPubDate(isset($post['published_at']) ? $post['published_at'] : ''); $item->setAuthor(isset($post['author']) ? $post['author'] : ''); $item->setCategory(isset($post['category']) ? $post['category'] : ''); $item->setGuid(isset($post['guid']) ? $post['guid'] : ''); $item->setSource(isset($post['source']) ? $post['source'] : []); $item->setEnclosure(isset($post['enclosure']) ? $post['enclosure'] : []); $feed->addItem($item); } $feed->save('public/feed.xml');
Result:
<?xml version="1.0" encoding="utf-8"?> <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"> <channel> <title>RSS Feed Title</title> <link>https://website.com</link> <description>Recent articles on your website</description> <language>en</language> <lastBuildDate>Tue, 28 Mar 2023 19:45:47 +0200</lastBuildDate> <copyright>MyWebsite.com</copyright> <image> <title>Image title</title> <url>https://website.com/Image.jpg</url> <link>https://website.com</link> </image> <item> <title> <![CDATA[Post title #1]]> </title> <link>https://website.com/1-post-title</link> <description> <![CDATA[Blog post about something very important]]> </description> <pubDate>Tue, 14 Mar 2023 12:00:00 +0100</pubDate> <guid isPermaLink="false">https://website.com/1-post-title</guid> <author>John Doe</author> </item> <item> <title> <![CDATA[Post title #2]]> </title> <link>https://website.com/2-post-title</link> <description> <![CDATA[Blog post number two]]> </description> <pubDate>Wed, 8 Mar 2023 16:30:00 +0100</pubDate> <guid isPermaLink="false">https://website.com/2-post-title</guid> <author>Jane Doe</author> </item> <item> <title> <![CDATA[Post title #3]]> </title> <link>https://website.com/3-post-title</link> <pubDate>Wed, 1 Mar 2023 08:45:00 +0100</pubDate> <guid isPermaLink="false">https://website.com/3-post-title</guid> <enclosure length="12345" type="audio/mpeg" url="https://website.com/podcasts/example.mp3"/> </item> </channel> </rss>