How to Create an RSS Feed Aggregator with PHP

How to Create an RSS Feed Aggregator with PHP

An RSS Feed Aggregator is a tool that consolidates content from various online sources into a single, easily accessible platform.

How to Create an RSS Feed Aggregator with PHP

RSS, which stands for Really Simple Syndication, is a format for delivering regularly changing web content. Websites and blogs often publish updates in RSS feeds, allowing users to subscribe and receive the latest content without visiting each site individually.

An RSS Feed Aggregator collects these feeds, presenting the content in a streamlined manner. This tool checks the subscribed RSS feeds for new content, retrieves the updates, and displays them for the user. It essentially acts as a personalized newsreader, gathering articles, blog posts, podcasts, and other types of web content in one place.

By using an RSS Feed Aggregator, users can stay informed about their favorite topics, websites, or authors without manually checking each source. This not only saves time but also ensures that users do not miss important updates. Popular RSS Feed Aggregators include Feedly, Inoreader, and The Old Reader, each offering various features to enhance the user experience. In essence, an RSS Feed Aggregator simplifies content consumption, making it more efficient and manageable for users.

Build Your Own RSS Feed Aggregator

With Only One PHP File

In this tutorial, you will learn how to create a simple RSS Feed Aggregator using PHP. This application allows users to add RSS feed URLs, remove them, and display the latest items from each feed. Let’s get started!

Prerequisites

Before you begin, ensure you have the following:

  • A web server with PHP support (e.g., XAMPP, WAMP, or a live server).
  • Basic knowledge of HTML, CSS, and PHP.

Step 1: Setting Up the Project

  1. Create a new directory for your project.
  2. Inside this directory, create the following files:
    • index.php
    • custom-urls.txt

Step 2: Building the HTML Structure

Let’s start by building the HTML structure in index.php:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<style>
body { font-size: 18px; }
a:visited { color: #a3bcd1; }
.header, .footer {
background-color: #f8f9fa;
padding: 20px;
text-align: center;
}
.header {
margin-bottom: 20px;
}
.footer {
margin-top: 20px;
}
.container {
max-width: 800px;
margin: auto;
}
h1, h2 {
font-size: 2em;
}
ul.list-group {
margin-bottom: 20px;
}
.list-group-item {
display: flex;
justify-content: space-between;
align-items: center;
}
</style>
<title>RSS Feed Aggregator</title>
</head>
<body>
<div class="header">
<h1>RSS Feed Aggregator</h1>
</div>
<div class="container">
<div class="row">
<div class="col-md-12">
<form method="post" action="">
<div class="form-group">
<label for="rss_url">Add RSS Feed URL:</label>
<input type="url" class="form-control" id="rss_url" name="rss_url" placeholder="Enter RSS feed URL" required>
</div>
<button type="submit" name="add_url" class="btn btn-primary">Add URL</button>
</form>

Step 3: Adding PHP Logic

Next, we’ll add PHP logic to handle the addition and removal of RSS feed URLs. Place the following PHP code after the form:

<?php


// Enable error reporting
error_reporting(E_ALL);
ini_set('display_errors', 1);

$customUrlsFile = "./custom-urls.txt";

// Handle URL addition
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['add_url'])) {
if (isset($_POST['rss_url'])) {
$rss_url = $_POST['rss_url'];

if (filter_var($rss_url, FILTER_VALIDATE_URL)) {
// Save the new URL to the file
if (file_put_contents($customUrlsFile, $rss_url . PHP_EOL, FILE_APPEND | LOCK_EX) === false) {
echo "<div class='alert alert-danger'>Failed to write to file</div>";
} else {
echo "<div class='alert alert-success'>URL successfully added to the file</div>";
}
} else {
echo "<div class='alert alert-danger'>Invalid URL</div>";
}
}
}

// Handle URL removal
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['remove_url'])) {
if (isset($_POST['url_to_remove'])) {
$urlToRemove = $_POST['url_to_remove'];

if (file_exists($customUrlsFile)) {
$customUrls = file($customUrlsFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$updatedUrls = array_filter($customUrls, function($url) use ($urlToRemove) {
return trim($url) !== trim($urlToRemove);
});

if (file_put_contents($customUrlsFile, implode(PHP_EOL, $updatedUrls) . PHP_EOL, LOCK_EX) === false) {
echo "<div class='alert alert-danger'>Failed to remove URL from file</div>";
} else {
echo "<div class='alert alert-success'>URL successfully removed from the file</div>";
}
}
}
}

// Display existing URLs
if (file_exists($customUrlsFile)) {
$customUrls = file($customUrlsFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

if (!empty($customUrls)) {
echo "<h2>Added URLs</h2><ul class='list-group'>";
foreach ($customUrls as $url) {
echo "<li class='list-group-item'>" . htmlspecialchars($url) . " ";
echo '<form method="post" action="" style="display:inline;">
<input type="hidden" name="url_to_remove" value="' . htmlspecialchars($url) . '">
<button type="submit" name="remove_url" class="btn btn-danger btn-sm">Remove</button>
</form></li>';
}
echo "</ul>";
} else {
echo "<div class='alert alert-info'>No URLs found in custom-urls.txt</div>";
}
}

Step 4: Fetching and Displaying RSS Feeds

Add the function to fetch and display the RSS feeds. Place the following PHP code at the end of the file, just before the closing </div> tags for the container and row:

<?php


// Function to fetch and display RSS feeds
function displayFeeds() {
$customUrlsFile = "./custom-urls.txt";
$newsSource = array();

// Load custom URLs from file
if (file_exists($customUrlsFile)) {
$customUrls = file($customUrlsFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($customUrls as $url) {
$newsSource[] = array(
"title" => $url,
"url" => $url
);
}
}

// Function to fetch and display feed
function getFeed($source) {
$url = $source['url'];
$html = "";
$rss = @simplexml_load_file($url);
if ($rss) {
$title = (string) $rss->channel->title;
if (empty($title)) {
$title = $source['title'];
}
$count = 0;
$html .= '<h2>' . htmlspecialchars($title) . '</h2>';
$html .= '<ul>';
foreach ($rss->channel->item as $item) {
$count++;
if ($count > 7) {
break;
}
$html .= '<li><a href="'.htmlspecialchars($item->link).'" target="_blank">'.htmlspecialchars($item->title).'</a></li>';
}
$html .= '</ul>';
} else {
$html .= "<p>Could not load feed from $url</p>";
}
return $html;
}

foreach ($newsSource as $source) {
echo getFeed($source);
}
}

// Display the feeds
displayFeeds();
?>
</div>
</div>
</div>
<div class="footer">
<p>© <?php echo date("Y"); ?> RSS Feed Aggregator by Your Name</p>
</div>
</body>
</html>

Step 5: Testing the Application

  1. Add URLs: Use the form to add new RSS feed URLs.
  2. Remove URLs: Use the “Remove” button to delete URLs.
  3. Verify Feeds: Check that the feeds are displayed with their site names, and the links open in a new tab.

RSS Feed Aggregator (4)

RSS Feed Aggregator (3) (1)

Conclusion

Congratulations! You’ve successfully built an RSS Feed Aggregator using PHP. This application allows you to add, remove, and display RSS feeds dynamically. Feel free to customize and extend this application as needed.

Share
Share
Website maintenance by: dp