getName() === 'feed') {
// Atom feed
$feedEntries = $feedData->entry;
} elseif ($feedData->getName() === 'rss') {
// RSS 2.0 feed
$feedEntries = $feedData->channel->item;
} else {
// Unknown feed format
continue;
}
foreach ($feedEntries as $entry) {
$entry->feedSource = $feed; // Add a property to track the source feed
$entries[] = $entry;
}
}
// Sort the entries by publish date
usort($entries, function ($a, $b) {
$aDate = strtotime($a->published ?? $a->updated ?? $a->pubDate);
$bDate = strtotime($b->published ?? $b->updated ?? $b->pubDate);
return $bDate - $aDate;
});
// Create a new EPUB file
$epub = new ZipArchive();
$epub->open('feeds.epub', ZipArchive::CREATE);
// Create the required directory structure
$epub->addEmptyDir('META-INF');
$epub->addEmptyDir('OEBPS');
// Create the container.xml file
$containerXml = '
';
$epub->addFromString('META-INF/container.xml', $containerXml);
// Create the content.opf file
$contentOpf = '
RSS Feeds Book
Author Name
urn:uuid:12345678-1234-1234-1234-123456789012
en
';
$epub->addFromString('OEBPS/content.opf', $contentOpf);
// Create the toc.ncx file
$tocNcx = '
RSS Feeds Book
RSS Feeds
';
$epub->addFromString('OEBPS/toc.ncx', $tocNcx);
// Create the content.html file
$contentHtml = '
RSS Feeds Book
';
foreach ($entries as $entry) {
$title = htmlspecialchars($entry->title, ENT_QUOTES, 'UTF-8');
$link = htmlspecialchars($entry->link, ENT_QUOTES, 'UTF-8');
$date = htmlspecialchars($entry->published ?? $entry->updated ?? $entry->pubDate, ENT_QUOTES, 'UTF-8');
$summary = htmlspecialchars($entry->summary ?? $entry->description, ENT_QUOTES, 'UTF-8');
$contentHtml .= '';
$contentHtml .= '' . $date . '
';
$contentHtml .= '' . $summary . '
';
}
$contentHtml .= '';
$epub->addFromString('OEBPS/content.html', $contentHtml);
// Close the EPUB file
$epub->close();
// Download the EPUB file
header('Content-Type: application/epub+zip');
header('Content-Disposition: attachment; filename="feeds.epub"');
header('Content-Length: ' . filesize('feeds.epub'));
readfile('feeds.epub');
exit;
?>