"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How Can I Efficiently Parse RSS/Atom Feeds in PHP Using SimpleXML?

How Can I Efficiently Parse RSS/Atom Feeds in PHP Using SimpleXML?

Published on 2024-11-24
Browse:974

How Can I Efficiently Parse RSS/Atom Feeds in PHP Using SimpleXML?

Parsing RSS/Atom Feeds with PHP

When parsing RSS or Atom feeds using Magpie RSS, it's important to consider alternative options that can handle well-formed feeds. One such option is SimpleXML.

SimpleXML, built into PHP, offers a user-friendly structure for parsing XML documents. It detects XML errors and warns upon encountering any issues. To address such errors, you can consider using HTML Tidy to clean up the source.

Here's a basic class that utilizes SimpleXML to parse RSS feeds:

class BlogPost {
    public $date;
    public $ts;
    public $link;
    public $title;
    public $text;
}

class BlogFeed {
    public $posts = [];

    public function __construct($file_or_url) {
        $file_or_url = $this->resolveFile($file_or_url);
        if (!$x = simplexml_load_file($file_or_url)) return;

        foreach ($x->channel->item as $item) {
            $post = new BlogPost();
            $post->date = (string)$item->pubDate;
            $post->ts = strtotime($item->pubDate);
            $post->link = (string)$item->link;
            $post->title = (string)$item->title;
            $post->text = (string)$item->description;
            $post->summary = $this->summarizeText($post->text);

            $this->posts[] = $post;
        }
    }

    private function resolveFile($file_or_url) {
        if (!preg_match('|^https?:|', $file_or_url))
            $feed_uri = $_SERVER['DOCUMENT_ROOT'] .'/shared/xml/'. $file_or_url;
        else
            $feed_uri = $file_or_url;

        return $feed_uri;
    }

    private function summarizeText($summary) {
        $summary = strip_tags($summary);
        $max_len = 100;
        if (strlen($summary) > $max_len)
            $summary = substr($summary, 0, $max_len) . '...';

        return $summary;
    }
}

By utilizing SimpleXML and handling XML errors, you can effectively parse both RSS and Atom feeds using PHP.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3