php get link from atom feed

Updated: 26th May 2023
Tags: php

If you want to parse atom xml and get link you’ll have work to do.

Atom feed xml has something like this

<link rel="alternate" type="text/html" href="https://example/post/777"></link>

To get the link we use

<?php
$link = $item->link['href'];

Full example getting both atom and simple rss

<?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;
}