dongzhuner6981 2011-12-08 00:35
浏览 90
已采纳

如何从PHP文件中获取RSS源或将PHP转换为xml

I created an RSS feed page with PHP and tried to run it through through a JavaScript RSS reader code (http://www.zazar.net/developers/jquery/zrssfeed/ )

It does not recognize the PHP file as having XML code. Though I copied the rendered code generated from PHP and added it in an XML file and it worked.

Is there a way to change this PHP file extension into an XML? Or make the PHP file visible to the above JavaScript code?

I tried using the .htaccess to change the file type (as there are tutorials all over covering that) but it did not work.

Can you recommend anything?

This is my code:

<?php
header ("Content-Type:text/xml");  
$xml = new SimpleXMLElement('<xml/>');

for ($i = 1; $i <= 8; ++$i) {
    $track = $xml->addChild('track');
    $track->addChild('path', "song$i.mp3");
    $track->addChild('title', "Track $i - Track Title");
}

print($xml->asXML());

?>
  • 写回答

2条回答 默认 最新

  • dsaf32131 2011-12-08 09:39
    关注

    Some environments do not manage files based on mime-types but just rely on the extension of the filename. I would change two things:

    • You can try to force the filename to have .xml / .rss extension.
    • Do not use 'xml' as a node name. Change it to 'playlist' or some other thing.

    So, the code could be something like this:

    <?php
    header ("Content-Type: text/xml");
    $filename = 'playlist.rss';
    header ("Content-Disposition: attachment; filename=$filename");
    $xml = new SimpleXMLElement('<playlist/>');
    ... // The rest of the code keeps as the original
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?