Table of Contents
Introduction
Want to collect news from multiple websites automatically? In this guide, you will learn how to build a PHP news aggregator step by step. This real-world project will help you understand how to fetch and combine data from multiple sources.
What is a News Aggregator?
A news aggregator collects articles from different websites and displays them in one place. It is widely used in news apps, dashboards, and monitoring tools.
How PHP News Aggregator Works
The process involves fetching multiple web pages, extracting article titles, and displaying them together.

Step 1: Define News Sources
<?php
$urls = [
"https://books.toscrape.com/",
"https://realpython.github.io/fake-jobs/"
];
?>
Step 2: Fetch Data Using cURL
<?php
function fetchData($url) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0");
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
?>
Step 3: Parse and Extract Titles
<?php
function extractTitles($html) {
$dom = new DOMDocument();
@$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$titles = $xpath->query("//h3/a");
$data = [];
foreach ($titles as $title) {
$data[] = $title->getAttribute("title");
}
return $data;
}
?>
Step 4: Combine Results
<?php
$allTitles = [];
foreach ($urls as $url) {
$html = fetchData($url);
$titles = extractTitles($html);
$allTitles = array_merge($allTitles, $titles);
}
foreach ($allTitles as $item) {
echo $item . "<br>";
}
?>
Understanding the Logic
This PHP news aggregator works by fetching multiple websites, extracting relevant data, and combining it into a single output.
Real-Life Use Cases
- News dashboards
- Content aggregation platforms
- Market monitoring tools
Common Mistakes
- Using incorrect XPath
- Scraping dynamic content
- Not handling errors
Pro Tips
- Use APIs where available
- Store data in a database
- Schedule updates using cron jobs
Limitations
Some websites block scraping or load content dynamically, which may require advanced tools.
FAQs
Can I aggregate news from any website?
Not all websites allow scraping. Always check their policies.
How to improve performance?
Use caching and limit the number of requests.
Can I store aggregated data?
Yes, storing data in MySQL is recommended for larger projects.
New to scraping? Start with our PHP web scraper guide.
Build automation tools like PHP price tracker.
Learn advanced techniques in PHP cURL scraping guide.
Conclusion
You now know how to build a PHP news aggregator. This is a powerful step toward building real-world automation tools.
Call to Action
Start building your own PHP news aggregator and explore automation opportunities.
Next Step
Continue learning by reading our PHP cURL scraping guide.
Note: This tutorial is for educational purposes. Always respect website terms before scraping.