PHP LogoSimplePie

SimplePie is an open-source PHP library designed to make it easy for developers to parse RSS and Atom feeds. It abstracts away the complexities of different feed formats, character encodings, and XML parsing, providing a consistent and user-friendly API to access feed content. Developed with speed and efficiency in mind, SimplePie supports caching mechanisms to reduce server load and improve performance by storing parsed feed data locally.

Key features of SimplePie include:

* Comprehensive Feed Support: It handles all major versions of RSS (0.9x, 1.0, 2.0) and Atom (0.3, 1.0) feeds, along with many common extensions.
* Automatic Discovery: Can automatically discover feeds from a given URL (e.g., a website's homepage) using standard auto-discovery links.
* Caching: Built-in caching system helps to reduce bandwidth usage and improve page load times by storing parsed feed data for a specified duration.
* Character Encoding Handling: Automatically detects and converts various character encodings to UTF-8, ensuring consistent display of international characters.
* Error Handling: Provides robust error reporting for malformed or unavailable feeds.
* Extensibility: Allows for easy extension and customization through hooks and methods.
* Security: Sanitizes feed content to prevent common XSS vulnerabilities.
* Multi-Feed Parsing: Capable of parsing multiple feeds simultaneously and merging their items into a single collection.

SimplePie simplifies the process of integrating external content into PHP applications, making it ideal for building news aggregators, blog readers, or any application that needs to display dynamic content from external sources.

Example Code

<?php

require_once(__DIR__ . '/simplepie.inc'); // Adjust path to your SimplePie autoloader

$feed = new SimplePie();

// Set the URL of the feed you want to parse
$feed->set_feed_url('http://rss.slashdot.org/Slashdot/slashdotMain');

// Optional: Set a cache location and duration (in seconds)
// Make sure this directory is writable by your web server
$feed->set_cache_location(__DIR__ . '/cache');
$feed->set_cache_duration(1800); // Cache for 30 minutes (1800 seconds)

// Initialize the SimplePie object. This fetches and parses the feed.
$feed->init();

// Check if the feed was initialized successfully
if ($feed->error()) {
    echo "<p class=\"error\">Error: " . $feed->error() . "</p>";
} else {
    // Display the feed title
    echo "<h1>" . $feed->get_title() . "</h1>";
    echo "<p>" . $feed->get_description() . "</p>";

    // Loop through each item in the feed
    foreach ($feed->get_items() as $item) {
        echo "<h2><a href=\"" . $item->get_permalink() . "\">" . $item->get_title() . "</a></h2>";
        echo "<p>Date: " . $item->get_date('j F Y, g:i a') . "</p>";
        echo "<p>" . $item->get_description() . "</p>";
        // Optionally, display categories or authors
        if ($categories = $item->get_categories()) {
            echo "<p>Categories: ";
            foreach ($categories as $category) {
                echo $category->get_label() . " ";
            }
            echo "</p>";
        }
    }
}

// To use this code:
// 1. Download SimplePie from its official GitHub repository or website.
// 2. Extract it into a directory (e.g., 'simplepie-library') in your project.
// 3. Make sure the 'require_once' path points to the 'autoloader.php' or 'simplepie.inc' file.
// 4. Create a writable 'cache' directory in the same location as your PHP script.
// 5. Run the PHP script via your web server.

?>