How to dynamically return XML data using PHP
This is the second part of a tutorial covering the Front-end browser part of Ajax’ing and the Back-end part of sending data with PHP. The first part can be found here.
So we’ve covered how to request an interpret XML data from a webpage using Sarissa. Now we want to control what data is being returned, dynamically.
The basic principal behind setting up the script is to make the browser think it’s an XML document, not a PHP script. There are 2 ways you can do this:
- Add a content-type header into your script
- Add an Apache handler to treat XML files as scripts (which still involves step 1 anyway)
So I’ll only cover the first option. On top of that, I’m going to throw you in the deep end here, and show you the entire script:
//simulate a remote connection
sleep(2);
//include any libraries you want to use here.
//get the $mode var
$mode = stripslashes($_GET['mode']);
//Set the content-type header to xml
header("Content-type: text/xml");
//echo the XML declaration
echo chr(60).chr(63).'xml version="1.0" encoding="utf-8" '.chr(63).chr(62);
?>
<xmlresponse>
<?php
//make a decision based on $mode
switch ($mode) {
case 'getitems':
//set items in a test array
$items_array[] = array(
'id'=>15,
'name'=>'Finger Bun',
'price'=>'$0.80 ea'
);
$items_array[] = array(
'id'=>16,
'name'=>'Donuts',
'price'=>'$0.50 ea'
);
$items_array[] = array(
'id'=>17,
'name'=>'Apple Pie',
'price'=>'$1.20 slice'
);
$items_array[] = array(
'id'=>18,
'name'=>'Double Choc Chip Cup Cakes',
'price'=>'$1.00 ea'
);
//echo a count of items
echo '<item_count>'.count($items_array).'</item_count>';
//echo the array in XML style
for ($x=0;$x<count($items_array);$x++) {
echo '<item>';
echo '<id>'.htmlspecialchars($items_array[$x]['id']).'</id>';
echo '<name>'.htmlspecialchars($items_array[$x]['name']).'</name>';
echo '<price>'.htmlspecialchars($items_array[$x]['price']).'</price>';
echo '</item>';
}
//set a No Error response - function defined below
SetErrorNode();
break;
default:
//inccorrect mode, let the hacker know!
SetErrorNode(404,'Stop hacking. Get a real job.');
break;
} //end switch
?>
</xmlresponse><?php
//FUNCTIONS
//this sets the error code node - essential for every xml document to be returned.
function SetErrorNode($code=0,$text='') {
echo "<error_code>" . $code . "</error_code>\n<error>" . htmlspecialchars($text) . "</error>";
}
?>
Here’s an explanation. First we simulate an internet connection delay (for those using local development servers) in case you’ve got any features such as a waiting screen on the front-end that you want to admire for two seconds.
Next we get the $mode variable from the GET array (as used in part one of this tutorial) so we can decide what to do with it later. Then we send the content-type headers to the browser, so that it knows this is an XML document. Then we echo the XML declaration. You might be wondering why I’ve used chr everywhere there. This is so we don’t have to turn off PHP short tags just to make this thing work, a tip for all you out there.
Then we echo the root tag, xmlresponse. I’m not sure if changing this will affect Sarissa’s interpretations, but I don’t change it anyway.
The rest of the script is echo’ing data for our application to use. The only thing to mention is that any data you want to send within XML tags should be coded using htmlspecialchars. Even HTML code should be coded. Sarissa will automatically convert it back for you on the front-end.
Finally, we use the function SetErrorNode to echo our error tags as we want to.
Well that’s all I’m going to cover this time around, but I’m sure this can help a lot of beginners out there. Leave a comment if you have any questions (like how to get the front-end to recognize more than 500 characters of data within a single tag, muahaha).

[...] « PHP Ajax File Browser How to dynamically return XML data using PHP » [...]
Sweet tutorial, just what I have been looking for! Well Pleased…
boy… what a hack.
thanks for the tutorial..
good site..
Thank you — This article helped me a-lot. I was having problems with “dynamically returning XML data to jquery .ajax javascript library using PHP and POST”. I was very close to a solution, and this allowed me to fix my problem.
Google did not find this article when I searched for “jquery xml php” or “javascript xml php”.
Can you please change two things so that g00gle might find it.
1) Change your title to “How to dynamically return XML to sarrisa.js javascript library using PHP”
2) Also change “Now we want to control what data is being returned, dynamically.” to “Now we want to use php server side code to dynamically generate xml data to be returned so that the javascript library sarrisa.js can process the response.”
Thanks
Dan