News aggregator php script

Here is a basic PHP script that aggregates news from multiple sources and displays them in a simple format:

<?php

// Configuration
$news_sources = array(
    'Source 1' => 'https://www.source1.com/rss',
    'Source 2' => 'https://www.source2.com/rss',
    'Source 3' => 'https://www.source3.com/rss',
);

// Function to fetch and parse RSS feed
function fetch_rss($url) {
    $xml = simplexml_load_file($url);
    $items = array();
    foreach ($xml->channel->item as $item) {
        $items[] = array(
            'title' => (string) $item->title,
            'link' => (string) $item->link,
            'description' => (string) $item->description,
            'pubDate' => (string) $item->pubDate,
        );
    }
    return $items;
}

// Function to aggregate news
function aggregate_news() {
    $news = array();
    foreach ($news_sources as $source => $url) {
        $rss_items = fetch_rss($url);
        foreach ($rss_items as $item) {
            $news[] = array(
                'source' => $source,
                'title' => $item['title'],
                'link' => $item['link'],
                'description' => $item['description'],
                'pubDate' => $item['pubDate'],
            );
        }
    }
    return $news;
}

// Display aggregated news
$news = aggregate_news();
echo '<h1>Latest News</h1>';
echo '<ul>';
foreach ($news as $item) {
    echo '<li>';
    echo '<h2>'. $item['title']. '</h2>';
    echo '<p>'. $item['description']. '</p>';
    echo '<p><a rel="nofollow" target="_blank" href="'. $item['link']. '">Read more</a></p>';
    echo '<p>Published: '. $item['pubDate']. '</p>';
    echo '</li>';
}
echo '</ul>';

?>

This script uses the simplexml_load_file function to fetch and parse the RSS feeds from each news source. It then aggregates the news items into a single array and displays them in a simple HTML list.

You'll need to modify the $news_sources array to include the URLs of the RSS feeds you want to aggregate. You can also customize the display format by modifying the HTML output.

Note that this script assumes that the RSS feeds are publicly accessible and do not require authentication. If the feeds require authentication, you'll need to modify the script to handle authentication.

Also, this script is just a basic example and may not work for all RSS feeds. You may need to modify the script to handle different RSS feed formats or to handle errors that may occur when fetching or parsing the feeds.

You can also use other libraries like Zend_Feed or MagpieRSS to parse the RSS feeds, they are more powerful and flexible than the built-in simplexml_load_file function.

You can also use caching mechanism to store the aggregated news, so that you don't have to fetch the feeds every time the script is run.

You can also add more features like filtering, sorting, and searching to the aggregated news.

Please keep in mind that this is just a basic example, and you should consider the terms of service of the news sources you are aggregating, and make sure you are not violating any copyright or other laws.