How to read rss or atom with php

Updated: 26th May 2023
Tags: php

<?php

$url = "https://exaample.com/feed/";
$feeds = simplexml_load_file($url);

// $feeds->channel->item is for rss (like is used in wordpress)
// $feeds->entry is for atom rss
$feedArray = $feeds->channel->item ?? $feeds->entry;

foreach ($feedArray as $i=>$item) {
    //$item->link['href'] is atom format. If not exist, use rss format
    $link = $item->link['href'] ?? $item->link;
    $title = $item->title;

    echo "$title $link" . PHP_EOL;
}