Easy Coding
  Forum Wiki Tagging Projekte Karte RSS
» Start
» All Recent Changes
» Wiki Suche
» Wiki Hilfe

Coder How To's

Algorithmen Informationen

edit SideBar

Neue Wiki Eintrage finden Sie unter easy-coding.de/wiki.

Ajax-RSS-Reader

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
  1. // original datei
  2. include_once 'simplepie.inc';
  3.  
  4. /**
  5. * json erwerterte pie
  6. */
  7. class SimplePieJson extends SimplePie {
  8.         protected $myPermalink;
  9.  
  10.         /**
  11.          * @param quelle -> Pfad zur RSS Quelle
  12.          * @param permalink -> Falls Details angefordert werden
  13.          */
  14.         public function __construct($quelle, $permalink=null) {
  15.                 parent::SimplePie($quelle);
  16.                 $this->handle_content_type();
  17.                 $this->myPermalink = $permalink;
  18.         }
  19.  
  20.         /**
  21.          * returns json string
  22.          */
  23.         public function __toString() {
  24.                 $row = array();
  25.  
  26.                 foreach($this->get_items() as $item) {
  27.                         $tmp = array();   
  28.  
  29.                         // Wurde nach Details gefragt?
  30.                         if($this->myPermalink !== null && $this->myPermalink == html_entity_decode($item->get_permalink())) {
  31.                                 $row['content'] = $item->get_content();
  32.                                 break;
  33.                         }
  34.  
  35.                         // Order reicht die Übersicht?
  36.                         else {
  37.                                 $tmp['permalink'] = $item->get_permalink();
  38.                                 $tmp['date'] = $item->get_date('d.m.Y H:i:s');
  39.                                 $tmp['title'] = $item->get_title();
  40.                                 $row[] = $tmp;
  41.                         }
  42.                 }
  43.  
  44.                 return json_encode($row);
  45.         }
  46. }

Die Benutzung wird dadurch denkbar einfach:

rss_json.php
  1. // GET Parameter abfragen
  2. $permalink = isset($_REQUEST['permalink']) ? $_REQUEST['permalink'] : null;
  3.  
  4. // Initialisation
  5. $pie = new SimplePieJson('http://www.easy-coding.de/index.php?page=Feed&type=Atom', $permalink);
  6.  
  7. // Ausgabe
  8. echo $pie;

Auch clientseitig brauchen wir nur eine For-Schleife:

rss.html
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" xml:lang="de">
  3.         <title>RSS Feed</title>
  4.         <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
  5.         <style type="text/css">
  6.         body { font-size:11px }
  7.         div { float:left; width:49%; border:1px solid #ff0000}
  8.         a { display:block; text-decoration:none; cursor:pointer }
  9.         a:hover { background-color:#efefef; }
  10.         </style>
  11.         <script type="text/javascript" src="jquery.js"></script>
  12.         <script type="text/javascript">
  13.         $.getJSON ("rss_json.php", {},
  14.                 function(json){
  15.                         for(var i=0; i<json.length; i++) {
  16.                                 $('#headlines').append('<a onclick="more(\''+json[i].permalink+'\')">'+json[i].title+' ('+json[i].date+')</a>');
  17.                         }
  18.                 }
  19.         );
  20.         function more(link) {
  21.                 $.getJSON ("rss_json.php", { permalink: link },
  22.                 function(json){
  23.                         $('#text').text(json.content);
  24.                 }
  25.         );
  26.         }
  27.         </script>
  28. </head>
  29.  
  30. <body>
  31.  
  32. <div id="headlines"></div>
  33. <div id="text"></div>
  34.  
  35. </body>
  36. </html>
Zuletzt geändert am 14.09.2007 23:40 Uhr
  Impressum