How to use simplexml_load_file to parse multiple feeds at once [duplicate] - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Monday, December 4, 2023

How to use simplexml_load_file to parse multiple feeds at once [duplicate]

Is it possible to extract the first item from a list of RSS feeds (ie from different sites) and display them together on one page, using PHP?

So far I'm using the code below, which works for a single feed, but I have no idea how to add more feeds and echo their first item. But I'd like to add more feeds besides verticalonline.

$rss_feed = simplexml_load_file("https://www.verticalonline.ro/feed");
if (!empty($rss_feed)) {
    $i = 0;
    
     foreach ($rss_feed->channel->item as $feed_item) {
        if ($i == 1) {
            break;
            } 
        else {
    echo " <item> \n";
    echo "  <title>".$feed_item->title."</title>\n";
    echo "  <link><![CDATA[ https://actualitate.org/stire-".preg_replace("(^https?://)", "", $feed_item->link )." ]]></link>\n";
    echo "  <category>".$feed_item->category."</category>\n";
    echo '<pubDate>'.$feed_item->pubDate.' GMT</pubDate>'."\r\n";     
    echo "  <description><![CDATA[ ".mb_strimwidth(trim(preg_replace(['/<[^>]*>/','/\s+/'],' ', $feed_item->description)), 0, 250, ' ...')." ]]></description>\n";        
if ($feed_item->enclosure)
echo '<enclosure url="'.$feed_item->enclosure->attributes()->url.'" type="image/jpeg" length="1967"/>'."\r\n";
else 
echo '<enclosure url="http://loremflickr.com/300/250/reporter?'.rand(0,25).'" type="image/jpeg" length="1967"/>'."\r\n"; 
     echo " </item>\n";
} 
        $i ++;
        }
}

I imagine I'll need an array, but this is above my noob php knowledge. Please help.



source https://stackoverflow.com/questions/77595489/how-to-use-simplexml-load-file-to-parse-multiple-feeds-at-once

No comments:

Post a Comment