Page 1 of 1

Generating XML through PHP????

Posted: Mon Jan 23, 2006 10:55 am
by ashleek007
feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


Hey everyone,

I'll describe the problem first of all.

Problem
I need to create an XML document containing variables taken from a MySql database passed through PHP 4.

I have the exact script in 'asp', shown below: -

Code: Select all

<%@LANGUAGE="JavaScript"%>
<%Response.ContentType="application/xml";%>
<?xml version="1.0"?>
<record>
<name><firstname><%=Response.Write("firstname")%></firstname> <lastname><%=Request.Form.item("lastname")%></lastname></name>
<address><line1><%=Request.Form.item("address1")%></line1>
<line2><%=Request.Form.item("address2")%></line2>
<city><%=Request.Form.item("city")%></city>
<state><%=Request.Form.item("state")%></state>
<postalcode><%=Request.Form.item("postalcode")%></postalcode>
<country><%=Request.Form.item("country")%></country>
</address>
</record>
This script pulls information from an HTML form and asp creates an xml document. My problem is that:-

1. My server doesnt accept 'asp'
2. My Database is MySQL
3. I'm rubbish at asp!

So the solution would be a PHP version of this script, or a method to create an XML document through PHP.

I hope/wish it is as easy as the asp version, but have heard rumours that i need to use somthing scary called XSLT?!

Any help will be much apreciated
Thanks,
Ash
:(


feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Re: Generating XML through PHP????

Posted: Mon Jan 23, 2006 11:58 am
by timvw
The following should get you started... It's possible form data is in $_POST instead of $_GET though (don't know which method yoru form is using.)

Code: Select all

<php header('Content-Type: application/xml'); ?>
<?xml version="1.0"?>
<record>
<name>
 <firstname><?php echo "firstname"; ?></firstname>
 <lastname><?php echo $_GET["lastname"]; ?></lastname>
</name>
.............

Posted: Mon Jan 23, 2006 1:47 pm
by AKA Panama Jack
This is a neat little program...

http://pxw4pa.sourceforge.net/index.php

It will take an array variable and covert it to XML output.

Posted: Mon Jan 23, 2006 5:34 pm
by ashleek007
wow, thanks you guys

i'm in bed at the minute,lol! but i'll have a crack at these tomorrow and let you all know how it goes.

If it works, i owe ya both a beer!

ta,
Ash

Posted: Mon Jan 23, 2006 6:19 pm
by Jenk
Depending on your data structure..

Code: Select all

<?php
$result = mysql_query('SELECT * FROM `table`');

echo '<?xml ...'; //blah blah

while ($row = mysql_fetch_assoc($result)) {
    echo '<record>';
    foreach ($row as $cell => $val) {
        echo "\t<{$cell}>{$val}</{$cell}>\n";
    }
    echo '</record>';
}
?>

Posted: Tue Jan 24, 2006 7:12 am
by ashleek007
hey,

tried the first solution: -

Code: Select all

<?php header('Content-Type: application/xml'); ?> 
<?xml version="1.0"?> 
<record> 
<name> 
<firstname><?php echo "firstname"; ?></firstname> 
<lastname><?php echo "lastname"; ?></lastname> 
</name>
this threw up an error: -

Parse error: parse error, unexpected T_STRING in e:\domains\2\2unique.co.uk\user\htdocs\xml\phpXML.php on line 2.

Not sure why this problem occurs?

I'll try the second solution when i get home from work.

The third solution would be an easier way i gues, basically faking the XML document? not sure if this is a nice way to do it though?

Any ideas would help thanks,
Ash
[/quote]

Posted: Tue Jan 24, 2006 8:21 am
by eyespark
This is how I create my rss.xml files. If that helps.

Code: Select all

if($action == "rss"){
$result = mysql_query("SELECT * FROM articles ORDER BY aid DESC") or die(mysql_error());
$row = mysql_fetch_assoc($result);
$timestamp = date("r",$row['timestamp']);
$filename = "rss.xml";
$header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
$header .= "<rss version=\"2.0\">\n";
$header .= "<channel>\n";
$header .= "<title>some title here</title>\n";
$header .= "<link>http://www.mydomain.com</link>\n";
$header .= "<description>Site description</description>\n";
$header .= "<language>sl</language>\n";
$header .= "<lastBuildDate>". $timestamp ."</lastBuildDate>\n";
$header .= "<copyright>Copyright 2006</copyright>\n";
$header .= "<ttl>60</ttl>\n";
$footer = "</channel>\n";
$footer .= "</rss>";
$fh = fopen($filename, "w+");
fwrite($fh, $header);
do {
	$datum = date("r",$row['timestamp']);
	$items = "<item>\n";
	$items .= "<title>". stripslashes($row['title']) ."</title>\n";
	$items .= "<link>http://www.mydomain/index.php?action=article&aid=". $row['aid'] ."</link>\n";
	$items .= "<description>". stripslashes($row['intro']) ."</description>\n";
	$items .= "<pubDate>". $datum ."</pubDate>\n";
	$items .= "</item>\n";
	fwrite($fh, $items);
} while ($row = mysql_fetch_assoc($result));
fwrite($fh, $footer);
fclose($fh);
echo "<script>window.open(\"rss.xml\");</script>";
}

Posted: Tue Jan 24, 2006 9:07 am
by Maugrim_The_Reaper
In your PHP ini file you will need to disable the short_open_tag option. Its good practice to ensure (as you've done) to use <?php not <? for PHP.

This will prevent the opening XML tag being parsed as PHP in error. Its currently being interpreted as PHP because <? is the PHP short tag.