PHP code example of forensic / feed-parser

1. Go to this page and download the library: Download forensic/feed-parser library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

forensic / feed-parser example snippets


//include composer autoload.php
ule
use Forensic\FeedParser\Parser;

//create an instance
$parser = new Parser();

//parse yahoo rss news feed
$feed = $parser->parseFromURL('https://www.yahoo.com/news/rss/mostviewed');

//access feed properties
echo $feed->type; //1 for RSS, 2 for ATOM, 3 for RDF
echo $feed->title;
echo $feed->link;
echo $feed->lastUpdated;
echo $feed->generator;
echo $feed->description;
echo $feed->image->src;
....

//access items
$items = $feed->items;

foreach ($items as $item)
{
    //access feed item properties
    echo $item->link;
    echo $item->content;
    echo $item->textContent; // the same as content, but all html tags are stripped out
    echo $item->createdAt;
    echo $item->lastUpdated;
    echo $item->category;
    echo $item->image->src;
    echo $item->image->title;
    .....
}

//create an instance
$parser = new Parser();

//parse yahoo rss news feed
$feed = $parser->parseFromURL('https://www.yahoo.com/news/rss/mostviewed');

header('Content-Type: application/json');

//export whole feed
echo $feed->toJSON();

//export a single item
echo $feed->items[0]->toJSON();

//constructor signature
new Parser(
    string $default_lang = 'en',
    bool $remove_styles = true,
    bool $remove_scripts = true
);

  $parser = new Parser();
  $parser->setDefaultLanguage('fr');
  

  $parser = new Parser();
  $parser->removeStyles(true);
  

  $parser = new Parser();
  $parser->removeScripts(true);
  
bash
git clone https://github.com/teclone/php-feed-parser && php-feed-parser