用PHP动态创建xml文件(一般策略)


Creating XML-file with PHP dynamically (general strategy)

我的网站的核心是一个xml文件master.xml。我正试图从中创建提要,一个经典的rss提要和一个播客提要,这意味着XML到XML。

我尝试的第一种方法是用PHP的DomDocument创建一个真正的xml文件,但我遇到了基本问题。即使它工作正常,它仍然会创建一个文件,而不是正在请求的文件。

现在,我已经将podcast.xml更改为podcast.php,并使用项的循环回显xml声明和xml标记。像iTunes这样的阅读器在这方面做得很好。但是浏览器是不会被骗的。

  1. Chrome将内容放入伪html文件中,并显示纯文本,忽略xsl-stylesheet。尽管如此,它在代码视图中只显示XML。

  2. Firefox显示它像一个真正的xml文件,但在代码视图中,它显示关闭红色,好像是一个标记错误(事实并非如此)。

这让我(作为一个有抱负的人)想知道专业人士如何处理动态创建提要的愿望。当然,我不是第一个这样做的人。

我为此创建了一个小的开源库。

你可以在这里下载并学习如何使用它。

下面是一个例子:

use iTunesPodcastFeed'Channel;
use iTunesPodcastFeed'FeedGenerator;
use iTunesPodcastFeed'Item;
require __DIR__ . '/vendor/autoload.php';
// SETUP CHANNEL
$title = 'Read2Me Daily Curated Articles';
$link = 'https://read2me.online';
$author = 'NYTimes and Medium';
$email = 'hello@read2me.online';
$image = 'https://d22fip447qchhd.cloudfront.net/api/widget/static/images/default-thumbnail.png';
$explicit = false;
$categories = [
    'News',
    'Technology',
    'Culture',
    'Entrepreneurship',
    'Productivity'
];
$description = 'Daily curated articles from New York Times and Medium';
$lang = 'en';
$copyright = 'The New York Times Company and The Medium Company';
$ttl = 43200; // 12 hours in seconds
$channel = new Channel(
    $title, $link, $author, $email,
    $image, $explicit, $categories,
    $description, $lang, $copyright, $ttl
);
// SETUP EPISODE
$title = "Trump Says Disclosure of Mueller Questions in Russia Probe Is ‘Disgraceful’";
$fileUrl = 'https://s3.read2me.online/audio/www-nytimes-com-2018-05-01-us-politics-trump-mueller-russia-questions-html-7e9601.mp3';
$duration = '2:18';
$description = 'WASHINGTON — President Trump on Tuesday said it was “disgraceful” that questions the special counsel would like to ask him were publicly disclosed, and he incorrectly noted that there were no questions about collusion. The president also said collusion was a “phony” crime.';
$date = 1525177808;
$filesize = 828387;
$mime = 'audio/mpeg';
$item = new Item(
    $title, $fileUrl, $duration,
    $description, $date, $filesize, $mime
);
$item2 = clone $item; // just to give you an idea of how it works
// SETUP FEED
$feed = new FeedGenerator($channel, ...[$item, $item2]);
// OUTPUT XML
header('Content-Type: application/xml; charset=utf-8');
print $feed->getXml();