[ 'method' => 'GET', 'header' => "User-Agent: Mozilla/5.0\r\n" ], 'ssl' => [ 'verify_peer' => false, 'verify_peer_name' => false ] ]); $data = @file_get_contents( $url, false, $context ); if($data){ file_put_contents($cacheFile, $data); return $data; } return false; } function mapCategory($item){ $cats = []; if(isset($item->category)){ foreach($item->category as $c){ $clean = trim((string)$c); if($clean !== ''){ $cats[] = $clean; } } } return $cats ?: ['General']; } function parseFeeds($feeds, $dir, $ttl){ $items = []; foreach($feeds as $feed){ $xmlStr = fetchFeed( $feed, $dir.'/'.md5($feed).'.xml', $ttl ); if(!$xmlStr) continue; $xml = @simplexml_load_string($xmlStr); if(!$xml) continue; $entries = $xml->channel->item ?? $xml->entry ?? []; foreach($entries as $i){ $link = isset($i->link['href']) ? (string)$i->link['href'] : (string)$i->link; $desc = isset($i->summary) ? (string)$i->summary : (string)$i->description; $items[] = [ "title" => (string)$i->title, "link" => $link ?: '#', "desc" => strip_tags($desc), "date" => (string)( $i->pubDate ?? $i->updated ), "source" => preg_replace( '/^www\./', '', parse_url( $link, PHP_URL_HOST ) ), "categories" => mapCategory($i) ]; } } // fallback if(empty($items)){ $items[] = [ "title" => "News unavailable", "link" => "#", "desc" => "RSS feeds failed to load.", "date" => date('r'), "source" => "system", "categories" => ["System"] ]; } // newest first usort($items, function($a,$b){ return strtotime($b['date']) - strtotime($a['date']); }); return $items; } if(isset($_GET['api'])){ header('Content-Type: application/json'); echo json_encode( parseFeeds( $FEEDS, $CACHE_DIR, $CACHE_TTL ) ); exit; } ?>