Creating XML within PHP page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

alexus
Forum Contributor
Posts: 159
Joined: Fri Jul 04, 2003 10:49 pm

Creating XML within PHP page

Post 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!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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;
:?
alexus
Forum Contributor
Posts: 159
Joined: Fri Jul 04, 2003 10:49 pm

Post 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!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
alexus
Forum Contributor
Posts: 159
Joined: Fri Jul 04, 2003 10:49 pm

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
alexus
Forum Contributor
Posts: 159
Joined: Fri Jul 04, 2003 10:49 pm

Post 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; 
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
alexus
Forum Contributor
Posts: 159
Joined: Fri Jul 04, 2003 10:49 pm

Post by alexus »

thanks

as to the function, wouldnt it be the same since it is not going to be generated?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
alexus
Forum Contributor
Posts: 159
Joined: Fri Jul 04, 2003 10:49 pm

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I don't understand what you're asking.
alexus
Forum Contributor
Posts: 159
Joined: Fri Jul 04, 2003 10:49 pm

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you could call php's command line, which can run the file from there...
alexus
Forum Contributor
Posts: 159
Joined: Fri Jul 04, 2003 10:49 pm

Post by alexus »

i never did that how does that work?
Im just looking for easiest and most dynamic solution
Post Reply