开发者

Can PHP be used inside an XML file?

I am trying to generate a RSS feed from a mysql database I already have. Can I use PHP in the XML file that is to be sent to the user so that it generates the content upon request? Or should I use cron on the PHP file and generate an xml file? Or should I add the execution of the php file that generates the xml upon submitting the content that is to be used in the RSS? What do you think is the best pr开发者_如何学Goactice?


All three approaches are technically possible. However, I would not use cron, because it delays the update process of your XML-files after the database content has changed.

You can easily embed PHP-Code in your XML-files, you just have to make sure that the files are interpreted as PHP on the serverside, either by renaming them with a *.php extension or by changing the server directives in the .htaccess-file.

But I think that the best practice here is to generate new XML-files upon updating the database contents. I guess that the XML-files are viewed more often than the database content changes, so this approach reduces the server load.


Use a cron to automate a PHP script that builds the XML file. You can even automate the mail part as well in your PHP.


The third method you mentioned. I don't understand how cron can be used here, if there are data coming in users' request. The first method cannot be implemented.


Set the Content-type header to text/xml and have your PHP script generate XML just as it would generate any other content. You may want to consider using caching though, so you don't overwhelm the server by accident.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜