pub / kontra

Der linke Newsaggregator.
git clone src.jayvii.de/pub/kontra.git
Home | Log | Files | Exports | Refs | README | RSS

parse_rss.php (1562B)


      1 <!--
      2 SPDX-License-Identifier: AGPL-3.0-or-later
      3 SPDX-FileCopyrightText: 2025 JayVii <jayvii+kontra[AT]posteo[DOT]de>
      4 -->
      5 <?php
      6 
      7 function parse_rss($url, $categories, $name) {
      8 
      9     /* gather XML from URL */
     10     $xml = simplexml_load_file($url);
     11 
     12     /* parse every item from XML */
     13     $out = array();
     14 
     15     if (!is_null($xml->channel->item)) {
     16         for ($i = 0; $i < count($xml->channel->item); $i++) {
     17 
     18             /* fetch required objects */
     19             $title = (string) $xml->channel->item[$i]->title;
     20             $link = (string) $xml->channel->item[$i]->link;
     21             $description = (string) $xml->channel->item[$i]->description;
     22             $pubDate = (string) $xml->channel->item[$i]->pubDate;
     23             $timestamp = (string) strtotime($pubDate);
     24 
     25             /* insert objects into array*/
     26             array_push(
     27                 $out,
     28                 array(
     29                     "publ" => $name,
     30                     "cats" => $categories,
     31                     "title" => $title,
     32                     "link" => $link,
     33                     "desc" => $description,
     34                     "date" => $pubDate
     35                 )
     36             );
     37 
     38         }
     39     }
     40 
     41     /* return final array */
     42     return($out);
     43 
     44 }
     45 
     46 /* Sort by date */
     47 function sort_by_date($a, $b) {
     48     $at = strtotime($a["date"]);
     49     $bt = strtotime($b["date"]);
     50     if ($at > $bt) {
     51         return(+1);
     52     }
     53     if ($at < $bt) {
     54         return(-1);
     55     }
     56     if ($at == $bt) {
     57         return(0);
     58     }
     59 }
     60 function sort_by_date_rev($a, $b) {
     61     return(sort_by_date($a, $b) * (-1));
     62 }
     63 
     64 ?>
     65