Output dynamic user specific XML content via Drupal node
My set up is Drupal 6 on LAMP (Linux/Apache/MySQL/PHP) and I needed to dynamically generate XML on a per user basis as a data transfer mechanism between MySQL and the web browser via PHP and Java (I.e. AJAX) for a mapping application.
The actual code is pretty simple really, but finding the right place to put all the bits was very frustrating. I couldn't find an article that really nailed it, so here is what I did.
There are a number of elements needed to achieve this.
* Linking to drupal user managment (getting access to global $user).
* Creating a theme that doesn't mess with the XML.
* Writing PHP code to dynamically generate the XML.
* Writing JavaScript code to get and parse the XML (AJAX)
So here are some more details.
To serve that XML in a dynamic, user specific and secure way I needed to get access to the drupal userid requesting the data, this is why it had to be served in the form of a drupal node. So I found a basic theme Zen and sub themed it with Zenophile and eventually worked out that I needed three additional files, template.php, page.tpl.php, node.tpl.php, in that directory and to remove everything from them except the following:
In template.php:
In page.tpl.php and node.tpl.php:
So with that done there are a few more minor steps to get the node right.
* Ensure you add this to the body (obviously input format must be php) where /foo/bar/xml.php is your php to generate some XML.
* I also set a URL path setting to make it nicer code to read for the JavaScript URL.
Write some PHP code to generate an XML PHP domdocument.
Here is a very basic example of what you could do in your xml.php file:
uid == 2 ) {
$authorised = "yes";
}
$dom = new DOMDocument();
$root = $dom->createElement( 'foo' );
$dom->appendChild($root);
$bar = $dom->createElement( 'bar' );
$bar->setAttribute('authorised', $authorised);
$root->appendChild($bar);
echo $dom->saveXML(); ?>
Use ThemeKey module and set the drupal node to the new theme.
For the Javascript AJAX I used examples from these links:
Google Maps Example.
Download URL Function.
Comments