Skip to content

Instantly share code, notes, and snippets.

@poef
Last active January 31, 2016 12:15
Show Gist options
  • Save poef/dd977430dbf922d7555f to your computer and use it in GitHub Desktop.
Save poef/dd977430dbf922d7555f to your computer and use it in GitHub Desktop.
A very basic RSS reader using arc\http, arc\cache, arc\xml and arc\html
<?php
/**
* This is a simple demo showing how you can use different components from ARC together
* You can get arc at https://github.com/Ariadne-CMS/arc-arc/ or with composer:
*
* composer create-project arc/arc
*/
// load the composer autoloader
require __DIR__ . '/vendor/autoload.php';
// set shorter aliases for the html and xml writer/parser
use \arc\html as h;
use \arc\xml as x;
// create a http proxy with caching based on the cache headers of each request
$client = \arc\cache::proxy( \arc\http::client(), function($params) {
return ( \arc\http\headers::parseCacheTime( $params['target']->responseHeaders ) );
});
// get a rss feed
$feed = $client->get('http://rss.slashdot.org/Slashdot/slashdotMain');
// parse it, try xml first, but a lot of RSS feeds aren't valid XML
try {
$rss = x::parse($feed);
} catch( \Exception $e ) {
// so in that case parse it as HTML, the ARC API is the same
$rss = h::parse($feed);
}
// select all 'item' elements, using a CSS selector
$items = $rss->find('item');
// write valid HTML
foreach ($items as $item ) {
echo h::article(
h::h2( h::a( [ 'href' => $item->link->nodeValue ], $item->title->nodeValue ) ),
h::div( [ 'class' => 'body' ], $item->description->nodeValue )
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment