Creating XML within PHP page
Moderator: General Moderators
Creating XML within PHP page
Hi,
I’m trying to create an php page that will be displayed and serve as an XML file. How can I do that? (DOM objects are not installed)
The purpose of his is: is to make web servers talk to each other, but without any security trusts setup. For example one web server wants to find out what mirrors are up, so it goes to central server and request XML file that is generated dynamically from database by PHP. That XML contains IPs of all current mirrors, never the less external server never accesses the DB directly, and does not know password for DB.
So it should be something like I request servers.php, but on the screen is outputted regular XML, and that other server treat that php file as an XML file… I do not want to generate separate XML file.
Any ideas? Maybe alternative solutions? Thanks!
I’m trying to create an php page that will be displayed and serve as an XML file. How can I do that? (DOM objects are not installed)
The purpose of his is: is to make web servers talk to each other, but without any security trusts setup. For example one web server wants to find out what mirrors are up, so it goes to central server and request XML file that is generated dynamically from database by PHP. That XML contains IPs of all current mirrors, never the less external server never accesses the DB directly, and does not know password for DB.
So it should be something like I request servers.php, but on the screen is outputted regular XML, and that other server treat that php file as an XML file… I do not want to generate separate XML file.
Any ideas? Maybe alternative solutions? Thanks!
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Code: Select all
header('Content-type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8" ?>' . PHP_EOL;
echo '<tag>hi</tag>' . PHP_EOL;- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
PHP_EOL: http://php.net/reserved.constants it's the PHP server OS' new line, be it \r, \n or \r\n
As for XML loading, there's many available from DOMXML to DOM to SimpleXML for "built-in" stuff. If those aren't available, you can use a mutlitude of classes available on sites like phpclasses.org that can create "simple" structures of XML data loaded from a source. There are PEAR classes too.
As for XML loading, there's many available from DOMXML to DOM to SimpleXML for "built-in" stuff. If those aren't available, you can use a mutlitude of classes available on sites like phpclasses.org that can create "simple" structures of XML data loaded from a source. There are PEAR classes too.
I looked at the posibilities of getting XML to PHP variables and so far I thin i have to go with SimpleXML, but in all the cases that are in the manual they are using variable that is assigned XML, and then prossesed by SimpleXML...... so I though to use fopen to get my XML generated on another php page and assignn output to variable...
But when I'm using fopen im getting only "Resource ID #2" as an output...
SO what should I do to correct the problem?
But when I'm using fopen im getting only "Resource ID #2" as an output...
SO what should I do to correct the problem?
I was using file_get_contents() to get an XML file and it works fine, but when I was tried to load PHP file tat generates XML, i got the following error:
Whitespace is not allowed at this location. Error processing resource 'http://localhost/xml/load.php'. Line...
My PHP XML file is this:
Whitespace is not allowed at this location. Error processing resource 'http://localhost/xml/load.php'. Line...
My PHP XML file is this:
Code: Select all
<?
header('Content-type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8" ?>' . PHP_EOL;
echo '<server>' . PHP_EOL;
echo ' <authentication>' . PHP_EOL;
echo ' <ip>192.168.0.105</ip> ' . PHP_EOL;
echo ' <port>88</port> ' . PHP_EOL;
echo ' <category>USA</category>' . PHP_EOL;
echo ' </authentication>' . PHP_EOL;
echo '</server>' . PHP_EOL;
?>- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
file_get_contents(), along with all the other file system functions directly access the file. It does not execute the file to retrieve what would be output. If you want to pull the output from the file you can use an output buffering trick:However, I would recommend using a function to generate the XML so you can call it for reading or output as you wish.
Code: Select all
ob_start();
include('somefile.php'); // will execute the php inside, whether this is .php or .html or anything else, so be careful.
$output = ob_get_clean();I' actually thinking, that i might make a trap for myself now... since the idea was that FreeBSD would go and get the variables from XML file on another server... Now if FreeBSD would go there without internet browser, PHP would not be executed at all ( I think) so maybe what I should to is to make PHP to create actual XML file that will be updated whenever server list is changing?