|
1 | | -# php-feed-parser |
2 | | -A fully integrated atom, rss and rdf syndication feed parser for php |
| 1 | +# PHP Feed Parser |
| 2 | + |
| 3 | +PHP Feed Parser is a fully integrated syndication feed parser, with support for all major feed types including [RSS](http://cyber.harvard.edu/rss/rss.html), [ATOM](https://tools.ietf.org/html/rfc4287) & [RDF](http://web.resource.org/rss/1.0/spec) feeds. |
| 4 | + |
| 5 | +It produces clean, unified parse accross all the three supported feed types. It is configurable, lightweight and fully tested. |
| 6 | + |
| 7 | +It allows you to parse feeds from three different sources: |
| 8 | + |
| 9 | +1. From File using the `parseFromFile($file_abs_path)` method |
| 10 | +2. From URL using the `parseFromURL($url)` method |
| 11 | +3. From String using the `parseFromString($xml_string)` method |
| 12 | + |
| 13 | +## Install via Composer |
| 14 | + |
| 15 | +The project is available as a composer package. |
| 16 | + |
| 17 | +```bash |
| 18 | +composer require forensic/feed-parser |
| 19 | +``` |
| 20 | + |
| 21 | +setup your project to require composer's `autoload.php` file. |
| 22 | + |
| 23 | +## Getting Started |
| 24 | + |
| 25 | +```php |
| 26 | +//include composer autoload.php |
| 27 | +require 'vendor/autoload.php'; |
| 28 | + |
| 29 | +//use the projects Parser module |
| 30 | +use Forensic\FeedParser\Parser; |
| 31 | + |
| 32 | +//create an instance |
| 33 | +$parser = new Parser(); |
| 34 | + |
| 35 | +//parse yahoo rss news feed |
| 36 | +$feed = $parser->parseFromURL('https://www.yahoo.com/news/rss/mostviewed'); |
| 37 | + |
| 38 | +//access feed properties |
| 39 | +echo $feed->type; //1 for RSS, 2 for ATOM, 3 for RDF |
| 40 | +echo $feed->title; |
| 41 | +echo $feed->link; |
| 42 | +echo $feed->lastUpdated; |
| 43 | +echo $feed->generator; |
| 44 | +echo $feed->description; |
| 45 | +echo $feed->image->src; |
| 46 | +.... |
| 47 | + |
| 48 | +//access items |
| 49 | +$items = $feed->items; |
| 50 | + |
| 51 | +foreach ($items as $item) |
| 52 | +{ |
| 53 | + //access feed item properties |
| 54 | + echo $item->link; |
| 55 | + echo $item->content; |
| 56 | + echo $item->lastUpdated; |
| 57 | + echo $item->category; |
| 58 | + echo $item->image->src; |
| 59 | + echo $item->image->title; |
| 60 | + ..... |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +## Setting Parser Options |
| 65 | + |
| 66 | +You can pass in some configuration options when creating a Parser instance or even set them later. |
| 67 | + |
| 68 | +```php |
| 69 | +new Parser( |
| 70 | + string $default_lang = 'en', |
| 71 | + string $date_template = '', |
| 72 | + bool $remove_styles = true, |
| 73 | + bool $remove_scripts = true |
| 74 | +); |
| 75 | +``` |
| 76 | + |
| 77 | +## Modifying options later |
| 78 | + |
| 79 | +You can change options through the appropriate exposed method too. |
| 80 | + |
| 81 | +```php |
| 82 | +$parser = new Parser(); |
| 83 | + |
| 84 | +//set language |
| 85 | +$parser->setLanguage($lang); |
| 86 | + |
| 87 | +//set date Template |
| 88 | +$parser->setDateTemplate($date_template); |
| 89 | + |
| 90 | +//remove styles |
| 91 | +$parser->removeStyles(true); |
| 92 | + |
| 93 | +//remove scripts |
| 94 | +$parser->removeScripts(true); |
| 95 | +``` |
| 96 | + |
| 97 | +## Parser Options Explained |
| 98 | + |
| 99 | +- **default_lang**: This option sets the default feed language property to use should there be no language entry found in the xml document. |
| 100 | + |
| 101 | +- **date_template**: This option sets the date formatter template used when parsing feed date properties such as `lastUpdated`. the default format used is `'jS F, Y, g:i A'`. The formatter template should be a valid php date formatter argument. see [date](http://php.net/manual/en/function.date.php) for details. |
| 102 | + |
| 103 | +- **remove_styles**: This option states if stylings found in any feed item's content, should be stripped off. The stylings include html `style` element and attribute. Defaults to true. |
| 104 | + |
| 105 | +- **remove_scripts**: This option states if any scripting found in any feed item's content, should be stripped off. Scripting includes html `script` element and event handler `on-prefixed` element attributes such as `onclick`. Defaults to true. |
| 106 | + |
| 107 | +### Exporting feed to Array & JSON |
| 108 | + |
| 109 | +You can export parsed feed to array or json. This can also help you view all properties that are accessible parsed feed. |
| 110 | + |
| 111 | +```php |
| 112 | +//include composer autoload.php |
| 113 | +require 'vendor/autoload.php'; |
| 114 | + |
| 115 | +//use the projects Parser module |
| 116 | +use Forensic\FeedParser\Parser; |
| 117 | + |
| 118 | +//create an instance |
| 119 | +$parser = new Parser(); |
| 120 | + |
| 121 | +//parse yahoo rss news feed |
| 122 | +$feed = $parser->parseFromURL('https://www.yahoo.com/news/rss/mostviewed'); |
| 123 | + |
| 124 | +$feed_array = $feed->toArray(); |
| 125 | + |
| 126 | +//send to front end |
| 127 | +header('Content-Type: application/json'); |
| 128 | +echo $feed->toJSON(); |
| 129 | +``` |
0 commit comments