Hier wird erklärt wie ihr einen ganz simplen RSS Reader baut. Dabei sind Kenntnisse der Objektorientierung und von JQUERY benötigt.
Als erstes erweitere ich den populären PHP RSS Reader simplepie um JSON Funktionen.
SimplePieJson.class.php
// original datei
include_once 'simplepie.inc';
/**
* json erwerterte pie
*/
class SimplePieJson extends SimplePie {
protected $myPermalink;
/**
* @param quelle -> Pfad zur RSS Quelle
* @param permalink -> Falls Details angefordert werden
*/
public function __construct($quelle, $permalink=null) {
parent::SimplePie($quelle);
$this->handle_content_type();
$this->myPermalink = $permalink;
}
/**
* returns json string
*/
public function __toString() {
foreach($this->get_items() as $item) {
// Wurde nach Details gefragt?
if($this->
myPermalink !==
null &&
$this->
myPermalink ==
html_entity_decode($item->
get_permalink())) {
$row['content'] = $item->get_content();
break;
}
// Order reicht die Übersicht?
else {
$tmp['permalink'] = $item->get_permalink();
$tmp['date'] = $item->get_date('d.m.Y H:i:s');
$tmp['title'] = $item->get_title();
$row[] = $tmp;
}
}
return json_encode($row);
}
}
Die Benutzung wird dadurch denkbar einfach:
rss_json.php
// GET Parameter abfragen
$permalink =
isset($_REQUEST['permalink']) ?
$_REQUEST['permalink'] :
null;
// Initialisation
$pie = new SimplePieJson('http://www.easy-coding.de/index.php?page=Feed&type=Atom', $permalink);
// Ausgabe
Auch clientseitig brauchen wir nur eine For-Schleife:
rss.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" xml:lang="de">
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
body { font-size:11px }
div { float:left; width:49%; border:1px solid #ff0000}
a { display:block; text-decoration:none; cursor:pointer }
a:hover { background-color:#efefef; }
</style>
<script type="text/javascript" src="jquery.js"></script>
$.getJSON ("rss_json.php", {},
function(json){
for(var i=0; i<json.length; i++) {
$('#headlines').append('<a onclick="more(\''+json[i].permalink+'\')">'+json[i].title+' ('+json[i].date+')</a>');
}
}
);
function more(link) {
$.getJSON ("rss_json.php", { permalink: link },
function(json){
$('#text').text(json.content);
}
);
}
</script>
</head>
<body>
<div id="headlines"></div>
<div id="text"></div>
</body>
</html>