Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP动态创建XML文件(一般策略)_Php_Xml_Xslt_Rss_Feed - Fatal编程技术网

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

使用PHP动态创建XML文件(一般策略),php,xml,xslt,rss,feed,Php,Xml,Xslt,Rss,Feed,我网站的核心是一个XML文件master.XML。我正试图从中创建提要,一个经典的rss提要和一个播客提要,这意味着XML到XML的转换 我尝试的第一种方法是使用PHP的DomDocument创建一个真正的XML文件,但我遇到了一些基本问题。即使它工作正常,它仍然会创建一个不是所请求文件的文件 现在,我已经将podcast.xml更改为podcast.php,并使用一个项目循环来响应xml声明和xml标记。像iTunes这样的读者可以很好地理解这一点。但是浏览器不能真的被愚弄 Chrome将内容

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

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

现在,我已经将podcast.xml更改为podcast.php,并使用一个项目循环来响应xml声明和xml标记。像iTunes这样的读者可以很好地理解这一点。但是浏览器不能真的被愚弄

  • Chrome将内容放入伪HTML文件并显示纯文本,忽略xsl样式表。尽管如此,它在codeview中只显示XML

  • Firefox将其显示为一个真实的XML文件,但在codeview中,它以红色显示结束,就好像是一个标记错误(事实并非如此)


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

    我已经为此创建了一个小型开源库

    您可以下载并学习如何使用它

    下面是一个例子:

    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();
    

    我已经为此创建了一个小型开源库

    您可以下载并学习如何使用它

    下面是一个例子:

    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();
    

    DOM是一种有效且良好的方法(另一种方法是XMLWriter)。您应该描述DOM的问题。chrome中的XML视图已被破坏,即使它是具有正确内容类型的有效XML,也会显示为HTML。DOM方法会导致XMP组合。我曾经体验过Chrome的行为很奇怪,但它是一种流行的浏览器,我希望人们能够将提要视为一个样式化的页面(使用xsl和css)。您应该描述DOM的问题。chrome中的XML视图已被破坏,即使它是具有正确内容类型的有效XML,也会显示为HTML。DOM方法会导致XMP组合。我曾经体验过Chrome的行为很奇怪,但它是一款流行的浏览器,我希望人们能够将提要视为一个样式化的页面(使用xsl和css)。