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条)

报告相同问题?

悬赏问题

  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动