如何从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());
?>
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
?>
the Content-Type
header should by text/xml
Your XML should also adhere to RSS standards http://www.rssboard.org/rss-specification which it probably is doing since you can successfully open it when php is not rendering it.