including XML in page that causes PHP to crash
This might have been answered somewhere 开发者_运维问答else if so sorry. I have an dynamic XML document on a remote server that has the line <?xml version="1.0" encoding="UTF-8"?>
on the first line. This causes PHP to throw an exemption I'm guessing because of the ?>
My question is how do I include this document in my page without causing PHP to throw an exception?
Cheers
EDIT: I'm including by using require
, probably not the correct way to do it?
require
and include
are the wrong tools for this. Both include the page, yes - but then they attempt to run it as PHP code on your machine, without any security measures. Imagine what would happen if the remote machine returned <?php system('poweroff') ?>
- PHP on your machine would happily try to shutdown your server.
The function you're looking for is readfile() - it fetches the remote file and outputs it - without trying to run it as PHP.
This sounds like an issue with the short_open_tag
configuration option, which will interpret <?
as a PHP opening tag. You may want to try using the ini_set() function to set short_open_tag
to false
immediately before including the remote file, and then switch it back afterwards.
EDIT: If the file/URL you're referencing doesn't contain any PHP code that needs to be run, you can use the readfile() function to read and output its contents in one fell swoop. This will break if the file, as downloaded from the server, contains PHP snippets, but should work if the data is completely ready to go.
精彩评论