Page 1 of 2

Creating XML within PHP page

Posted: Mon Mar 27, 2006 5:24 pm
by alexus
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!

Posted: Mon Mar 27, 2006 5:45 pm
by feyd

Code: Select all

header('Content-type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8" ?>' . PHP_EOL;
echo '<tag>hi</tag>' . PHP_EOL;
:?

Posted: Tue Mar 28, 2006 1:06 pm
by alexus
Thanks a lot! So easy method!!!

What " PHP_EOL" does? Its not in the reference :-(

Maybe you also advoce me on the easiest way to read this "xml" file? as if it would be regualr XML? I just need to importa tag values to varable.


Thanks!

Posted: Tue Mar 28, 2006 1:22 pm
by feyd
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.

Posted: Wed Mar 29, 2006 1:42 pm
by alexus
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?

Posted: Wed Mar 29, 2006 2:05 pm
by feyd
fopen() itself returns a resource. You'll have to fread() from the file to get any data from the file's contents. Alternately, file_get_contents() works easier for most people.

Posted: Wed Mar 29, 2006 5:09 pm
by alexus
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:

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; 
?>

Posted: Wed Mar 29, 2006 5:20 pm
by feyd
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:

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();
However, I would recommend using a function to generate the XML so you can call it for reading or output as you wish.

Posted: Wed Mar 29, 2006 5:23 pm
by alexus
thanks

as to the function, wouldnt it be the same since it is not going to be generated?

Posted: Wed Mar 29, 2006 5:27 pm
by feyd
the function should not echo, instead it should return the output to display as a string that can then be output whenever the caller wishes, or not at all.

Posted: Wed Mar 29, 2006 5:32 pm
by alexus
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?

Posted: Wed Mar 29, 2006 6:24 pm
by feyd
I don't understand what you're asking.

Posted: Wed Mar 29, 2006 6:35 pm
by alexus
im rather thinking now... I just thouhjt that if i access PHP page through the comand line, the page will not be opened because PHP is not interpereted, and my server would do the rquest it through the comand line.... so I probably need to create XML filr rather then emitate it via PHP

Posted: Wed Mar 29, 2006 6:43 pm
by feyd
you could call php's command line, which can run the file from there...

Posted: Wed Mar 29, 2006 6:44 pm
by alexus
i never did that how does that work?
Im just looking for easiest and most dynamic solution