PHP Feed Parser is a fully integrated web syndication feed parser. It can successfully parse all the three major syndication feeds which include RSS, ATOM & RDF feeds.
It produces clean, unified parse accross all the three supported feed types. It is configurable, lightweight, fully tested and allows one to parse feeds from three different sources that includes parsing from url, from file or from string.
Install via composer:
composer require forensic/feed-parserCreate an instance as shown below:
//include composer autoload.phprequire'vendor/autoload.php';
//use the project's Parser moduleuseForensic\FeedParser\Parser;
//create an instance$parser = newParser();
//parse yahoo rss news feed$feed = $parser->parseFromURL('https://www.yahoo.com/news/rss/mostviewed');
//access feed propertiesecho$feed->type; //1 for RSS, 2 for ATOM, 3 for RDFecho$feed->title;
echo$feed->link;
echo$feed->lastUpdated;
echo$feed->generator;
echo$feed->description;
echo$feed->image->src;
....
//access items$items = $feed->items;
foreach ($itemsas$item)
{
//access feed item propertiesecho$item->link;
echo$item->content;
echo$item->textContent; // the same as content, but all html tags are stripped outecho$item->createdAt;
echo$item->lastUpdated;
echo$item->category;
echo$item->image->src;
echo$item->image->title;
.....
}You can also export the parsed feed as json, as shown below. This can also help you view all properties that are accessible in the parsed feed.
//create an instance$parser = newParser();
//parse yahoo rss news feed$feed = $parser->parseFromURL('https://www.yahoo.com/news/rss/mostviewed');
header('Content-Type: application/json');
//export whole feedecho$feed->toJSON();
//export a single itemecho$feed->items[0]->toJSON();The following configuration options can be passed in when creating an instance or set through the designated public methods:
//constructor signaturenewParser(
string $default_lang = 'en',
bool $remove_styles = true,
bool $remove_scripts = true
);default_lang:
This option sets the default feed language property to use should there be no language entry found in the xml document.
$parser = newParser(); $parser->setDefaultLanguage('fr');
remove_styles:
This option states if stylings found in any feed item's content, should be stripped off. The stylings include html
styleelement and attribute. Defaults to true.$parser = newParser(); $parser->removeStyles(true);
remove_scripts:
This option states if any scripting found in any feed item's content, should be stripped off. Scripting includes html
scriptelement and event handleron-prefixedelement attributes such asonclick. Defaults to true.$parser = newParser(); $parser->removeScripts(true);
To locally test or contribute to this project, You should have at least php 7.1, xDebug, composer and npm installed, then ollow the steps below:
Clone this repo:
git clone https://github.com/teclone/php-feed-parser && php-feed-parserInstall dependencies:
composer install && npm installRun test:
vendor/bin/phpunit