Wallogit.com
2017 © Pedro Peláez
Check rss feeds for new items
Check RSS feeds for new items, (*1)
CheckRSS provides some methods to:, (*2)
It is recommended using CheckRSS with a cron job so you can get new items periodically, although you can run it manually too., (*3)
composer require pqrs/checkrss=dev-master
Alternatively, add the dependency directly to your composer.json file:, (*4)
"require": {
"pqrs/checkrss": "dev-master"
}
Then add to your php code:, (*5)
``` php require_once DIR . '/vendor/autoload.php'; // Autoload files using Composer autoload, (*6)
use CheckRSS\RSS;, (*7)
## Usage ### method getItems($feed_url) This method gets **all the items** published in the rss feed and returns an object containing them. ``` php $rss = new RSS; $items = $rss->getItems("http://feeds.nbcnews.com/feeds/topstories");
An example of the object returned would be:, (*8)
SimpleXMLElement Object
(
[guid] => https://www.nbcnews.com/storyline/hurricane-irma/...
[title] => One-two punch of disease and Irma has left Florida citrus reeling
[pubDate] => Sat, 10 Feb 2018 15:17:00 GMT
[link] => https://www.nbcnews.com/storyline/hurricane-irma/...
[description] => Hurricane Irma served as a knockout punch for many of Florida's...
)
This method checks which items in a feed (obtained with getItems) are new since the last a check was made., (*9)
Returns an object with just the new items., (*10)
``` php $rss = new RSS;, (*11)
// Gets all the items published in the rss feed and stores them in $items $items = $rss->getItems("http://feeds.nbcnews.com/feeds/topstories");, (*12)
// Checks which items are new since last check if ($newitems = $rss->getNewItems($items) ) {, (*13)
// Prints new items
echo "New items found:" . PHP_EOL . PHP_EOL;
foreach ($newitems as $value) {
echo $value->title . PHP_EOL;
echo $value->description . PHP_EOL;
echo $value->guid . PHP_EOL;
echo $value->link . PHP_EOL. PHP_EOL;
}
} else {, (*14)
echo "There'are no new items in RSS feed";
} ```, (*15)
Internal use. This method writes the last read item id in a file to compare it in future checks., (*16)
Internal use. This method compares the last read item id with the last stored one in previous check., (*17)
A simple method to write a log file everytime a check is made., (*18)
You can find some uses for these functions in tests folder., (*19)
PHP 5.3.0, (*20)
Contributions are of course very welcome!, (*21)
Copyright © 2018 Alvaro Piqueras - pqrs, (*22)
This is a cleanup of a dirty code I wrote some time ago. Also it's my first try with GitHub, Composer and Packagist so if you find some mistakes or have some tips for me, I would be more than glad to hear you., (*23)
I hope this code suites your needs., (*24)
This project is licensed under the MIT License - see the LICENSE file for details., (*25)