alshf/hunter-dog

A lightweight, expressive and fast Feed RSS Reader. HunterDog takes care of value sanitization.

v1.1.2 2016-05-24 16:10 UTC

README

Build Status License Total Downloads Latest Stable Version Codacy Badge

HunterDog Feed Reader

A lightweight, expressive and fast Feed Reader. HunterDog takes care of value sanitization. At least PHP 5.4 is required.

Documentation

Installation

HunterDog uses Composer to make things easy.

Learn to use composer and run this Command Line:

composer require alshf/hunter-dog

Channels

HunterDog provides feed channels and you should use these channels for each feed URLs

Available Channels

	// Channels
	alshf\Channels\CNN::class
	alshf\Channels\Cnet::class
	alshf\Channels\Goal::class
	alshf\Channels\Quartz::class
	alshf\Channels\Forbes::class
	alshf\Channels\Skynews::class
	alshf\Channels\Telegraph::class
	alshf\Channels\TheGuardian::class
	alshf\Channels\TheIndependent::class
	alshf\Channels\BusinessInsider::class
	alshf\Channels\EveningStandard::class
	alshf\Channels\TheNewYorkTimes::class

How to use

First you should wake HunterDog up and give him a feed URL and a channel class, he will go and grab all feed items for you!

Make sure you have Composer's autoload file included

require 'vendor/autoload.php';

Step 1.

// use HunterDog & Exception
use alshf\HunterDog;
use alshf\Exceptions\HunterDogException;

try
{	
	// Create new instance from HunterDog class and pass URL and channel class as an array parameter
	$feed = new HunterDog([
		'url' 		=> 'http://cnet.com/rss/news/',
		'channel' 	=> alshf\Channels\Cnet::class,
	]);

	// Get feed
	$feed->get();
}
catch( HunterDogException $e )
{
	// Error
	echo $e->getMessage();
}

Step 2.

Now you can get title or description or etc from each feed items

use alshf\Exceptions\InvalidValueException;

Note: HunterDog will Sanitize it for you so you need to use Sanitizer exception

// use Sanitizer exception
use alshf\Exceptions\InvalidValueException;

// Loop throught each feed items
foreach ( $feed->get() as $item ) 
{
	try 
	{
		// New intance of stdClass for test
		$feed = new stdClass;

		// Get feed items properties
		$feed->title 	   	= $item->title;
		$feed->description 	= $item->description;
		$feed->link 	   	= $item->link;
		$feed->image 	 	= "<img src='".$item->image."'>";
		$feed->author   	= $item->author;
		$feed->publishedAt  = $item->publishedAt;
		$feed->guid 		= $item->guid;

		print_r($feed);
	} 
	catch (InvalidValueException $e) 
	{	
		// When HunterDog Sanitize each feed item property,
		// Throw exception on invalid string values or images
		continue;
	}
}

Error Handler

you can get all or last XML Error with ErrorBag trait.

// SadDog Facade
use SadDog;

// Get all Errors | return an Array of all Errors
SadDog::errors();

// Get last Error | return last Error string
SadDog::lastError();
Example
// use HunterDog & Exception
use alshf\HunterDog;
use alshf\Exceptions\HunterDogException;
use SadDog;

try
{	
	// Create new instance from HunterDog class and pass URL and channel class as an array parameter
	$feed = new HunterDog([
		'url' 		=> 'http://cnet.com/rss/news/',
		'channel' 	=> alshf\Channels\Cnet::class,
	]);
}
catch( HunterDogException $e )
{
	// use ErrorBag trait
	echo SadDog::lastError();
}

Contributing

Bugs and feature request are tracked on GitHub.

Credits

The code on which this package is principally developed and maintained by Ali Shafiee.

License

The HunterDog package is released under the MIT License.