Advertisement
Guest User

Lecture de flux RSS en PHP

a guest
Apr 27th, 2013
638
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.02 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * URLs des flux RSS
  5.  */
  6. $feeds = array(
  7.   array('website' => 'Alsacreations', 'feed_url' => 'http://www.alsacreations.com/rss/actualites.xml'),
  8.   array('website' => 'Developpez', 'feed_url' => 'http://www.developpez.com/index/rss'),
  9.   array('website' => 'Les intégristes', 'feed_url' => 'http://www.lesintegristes.net/articles/feed/'),
  10. );
  11.  
  12. /**
  13.  * Lecture des flux
  14.  */
  15. libxml_use_internal_errors(true);
  16.  
  17. $news = array();
  18.  
  19. foreach ($feeds as $feed) {
  20.     $raw_feed = simplexml_load_file($feed['feed_url']);
  21.    
  22.     /**
  23.      * Si le flux a pu être lu on le traite, sinon on passe au flux suivant
  24.      */
  25.     if (false !== $raw_feed) {
  26.      
  27.       $namespaces = $raw_feed->getNamespaces(true);
  28.       $raw_news = $raw_feed->xpath('/rss//item');
  29.  
  30.       /**
  31.        * On lit et on formatte la date
  32.        */
  33.       foreach($raw_news as $key => $item) {
  34.        
  35.         /**
  36.          * On récupére la date dans le noeud dc:date ou puDate, selon le format du flux
  37.          */
  38.         if (empty($item->pubDate) && isset($namespaces['dc'])) {
  39.           $item_date = $item->children($namespaces['dc']);
  40.           $date = new DateTime($item_date->date);
  41.         } else {
  42.           $date = new DateTime($item->pubDate);
  43.         }
  44.  
  45.         /**
  46.          * Formattage des données pour plus tard...
  47.          */
  48.          $raw_news[$key]->addChild('timestamp', $date->getTimestamp());
  49.          $raw_news[$key]->addChild('displayDate', $date->format('d/m/Y H:i:s'));
  50.         $raw_news[$key]->addChild('website', $feed['website']);
  51.       }
  52.  
  53.       /**
  54.        * On stocke les news dans un tableau global
  55.        */
  56.       $news = array_merge($news, $raw_news);
  57.      
  58.       unset($raw_feed, $raw_news);
  59.     }
  60. }
  61.  
  62. /**
  63.  * On trie les entrées par date (noeud timestamp)
  64.  */
  65. function cmp_timestamp($a, $b)
  66. {
  67.   return $b->timestamp - $a->timestamp;
  68. }
  69.  
  70. usort($news, 'cmp_timestamp');
  71.  
  72. /**
  73.  * On affiche le résultat dans le template.
  74.  */
  75. header('Content-Type: text/html; charset=UTF-8');
  76. ?>
  77. <!DOCTYPE html>
  78. <html lang="fr">
  79. <head>
  80.   <meta charset="utf-8" />
  81.   <title>Les news</title>
  82.  
  83.   <style>
  84.     body {
  85.       font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
  86.       font-size: 100%;
  87.     }
  88.    
  89.     .news-title, .news-source, .news-body {
  90.       padding: 15px;
  91.       margin: 15px;
  92.       background: #efefef;
  93.       border: 1px solid #aaaaaa;
  94.     }
  95.  
  96.     .news-title {
  97.       margin-top: 50px;
  98.     }
  99.    
  100.     hr {
  101.       margin: 15px;
  102.       border: 0;
  103.       background: #aaaaaa;
  104.       height: 15px;
  105.     }
  106.   </style>
  107. </head>
  108. <body>
  109.  
  110.   <h1>Les dernières news</h1>
  111.  
  112.   <?php foreach ($news as $post): ?>
  113.  
  114.   <h2 class="news-title">
  115.     <a href="<?php echo $post->link; ?>"><?php echo $post->title; ?></a>
  116.   </h2>
  117.  
  118.   <p class="news-source">
  119.     Publié le : <?php echo $post->displayDate; ?> sur <?php echo $post->website; ?>
  120.   </p>
  121.  
  122.   <div class="news-body">
  123.     <?php echo $post->description; ?>
  124.   </div>
  125.  
  126.   <hr>
  127.  
  128.   <?php endforeach; ?>
  129.  
  130. </body>
  131. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement