Jump to content
Science Forums

Passing Data to XML Via a Server-Side Language


 Share

Recommended Posts

There are a lot of tutorials and howto's out there that outline how to deal with XML data in a server-side language, however i have found few if any references of how to do the inverse, but what if you need to, and you can't just have your PHP generate the entire XML, what if the XML is driving a program on another server, but it's data driven, for example say you have a VoiceXML IVR and you want it to tell you how many tickets are in your queue, ticket system, php, runs on one server, lets say 10.0.0.1 and your IVR is hosted on another box all together, say 10.0.0.2, how do you pass the data using PHP on 10.0.0.1 to access the ticket system, on 10.0.0.2, a box that has no server-side scripting capability at all, and perhaps can only even reach 10.0.0.1 via port 80? (this took some figuring out)

 

Ok, first thing is, what i will be taking for a given, given is the user id that is available and somehow passed to the VoiceXML, though i guess you could use a similar technique, there is still some piece you will need to pass to tie the IVR on the remote box to your user, this may be user auth form that you have already gone through, or any other, and there are a few, way to get that data.

 

Second thing, i wont go into how one would get ticket data out of any ticket system, this is a completely impromptu example, i will assume that it's data that you can get via including a ticket_system.php file's function get_ticket_count, function that takes an argument of user_id.

 

All right, so first thing is first, lets build a short VoiceXML file:

<?xml version="1.0" encoding="UTF-8" ?>
<vxml version="2.1">

   <meta name="maintainer" content="[email protected]"/>

   <property name="timeout" value="5s"/>

   <var name="user_id" expr="''" /> <!-- I assume here that you assign user_id -->
   <var name="MyData"/> 
   <var name="tickets_in_queue">

   <form id="say_tickets">
       <prompt>You have <value expr="tickets_in_queue"> tickets in queue</prompt>
   </form>
</vxml>

 

It's fairly straight forward, if you've seen XML before, you pretty much understand what this will do, in terms of the IVR...

 

Now, how to populate tickets_in_queue, you can do it a couple of ways, one that i will discuss is using the <data> tag

 

typical <data> tag uses a uri to reference an XML file, the data from which gets imported into the current XML, however it does provide useful options, like namelist and method (not dissimilar to the <submit> tag) that allows you to pass data with getting the variable. So, adding the data tag (which can only be ran within a form), we add a new form:

   <form id="get_data">
       <block>
                          <data src="http://10.0.0.1/get_ticket_count.php" name="MyData" method="post" namelist="user_id"/>
                       <assign name="document.MyData" expr="MyData.documentElement"/>
           <goto next="#say_tickets"/>
       </block>
   </form>

Now what the assign does is it assignts the MyData variable the documenElement property of itself, which makes it a little easier to work with.

 

Before we progress, let's define how we will structure the XML that we pass to this, i think we can do something as simple as

 

<?xml version="1.0" encoding="UTF-8" ?>
<tickets> 
    <count></count>
</tickets>

 

So that is the format, which can be easily followed in PHP

<?php
header("Content-type: text/xml");
require_once("ticket_system.php");
$count = ($_POST['user_id']) ? get_ticket_count($_POST['user_id']) : 0;
echo "<?xml version="1.0" encoding="UTF-8"?>
<tickets>
<count>".$count."</count>
</tickets>";
?>

So this will return an xml page with the ticket count in the datastructure. So now lets go back to the XML and parse this input

 

<?xml version="1.0" encoding="UTF-8" ?>
<vxml version="2.1">

   <meta name="maintainer" content="[email protected]"/>

   <property name="timeout" value="5s"/>

   <var name="user_id" expr="''" /> <!-- I assume here that you assign user_id -->
   <var name="MyData"/> 
   <var name="tickets_in_queue">
   <form id="get_data">
       <block>
                          <data src="http://10.0.0.1/get_ticket_count.php" name="MyData" method="post" namelist="user_id"/>
                       <assign name="document.MyData" expr="MyData.documentElement"/>
           <goto next="#say_tickets"/>
       </block>
   </form>
   <form id="say_tickets">

    <!-- JS function gives us back the child of the parent object by name -->
    <script>
    <![CDATA[
         function GetData(d, t)  {       
           return (d.getElementsByTagName(t).item(0).firstChild.data);
   } 
         ]]> 
       </script>

       <assign name="tickets_in_queue" expr="GetData(MyData, 'count')"/>

       <prompt>You have <value expr="tickets_in_queue"> tickets in queue</prompt>
   </form>
</vxml>

 

And basically that's it, we have created a data request form that passes us values over post, we have made the response page that returns XML, and we have parsed that XML in the main XML file to give us access to dynamic data...

 

Hopefully this saves someone some frustration of how to do something like this, there really isn't a ton of documentation on this...

 

As always,

 

 

Enjoy

Link to comment
Share on other sites

Yeah, this is useful if you have 2 systems that are completely separate from each other, perhaps one of them is blackish boxish, and yet you need to somehow pass data between the two, and dont really have a server side language to handle it. XML is not bad, but it's so rigid sometimes, i think there is a reason why google has been using JSON, a lot easier to deal with then XML, about as flexible for storage, and is parsed in order of 100 times faster by PHP then XML is (sooomethin' for you to look at :) )

 

Also curious, as i write a ton of random code, what kinda apps, if you dont mind me asking?

Link to comment
Share on other sites

I use a lot of XML because .Net serializes objects to it without any code whatsoever, with lots of cool pseudocode to skip attributes in the class at will.

 

OTOH, I've had so much trouble with browsers--all of them, not just IE--deciding they don't like some weird sequence of XML in ajax calls, that I do anything transported over http in JSON. Thankfully also for .Net people, WCF will serialze all kinds of communications to JSON, again, without any special coding (and WCF just got a lot simpler in 4.0).

 

My bet is that the reason Google uses JSON is not because of any performance advantage in PHP parsing (100x? Really? REALLY? Are you suuuuuuuuuuuuuuuure? :confused: ) is because as my experience--and that of many others too--in having browsers choke on the dang thing makes it too much trouble to want to bother with.

 

Don't care if you don't like M$: folks in the real world use it. :evil: :cheer:

 

What counts in making a happy marriage is not so much how compatible you are, but how you deal with incompatibility, :phones:

Buffy

Link to comment
Share on other sites

I may have over-exaggerated, but it's significantly faster, maybe in order of 10 times, probably more. Simple truth is that in PHP XML has to be parsed either by hand with lib XML, and its a PAIN.5 or via an external PEAR library, such as SimpleXML, a PHP library. Json serialization is built into the language, and this is true for PHP, perl, C, Java, Lisp, so forth, libraries are either already built in or available, so, optimized C code, now, how much faster it runs, i don't know, you can judge that, but if you don't really need object serialization, and you don't have too much data, you can use JSON with much better effectiveness then XML. That said there is another side of the coin. JSON can't describe very complex objects as well as XML can, this is why YAML was created, but YAML, like XML in PHP requires external libraries. Another side is if you can't parse the whole data object at once, then lib xml may be beneficial, as you can split data, but neither json serialize nor the SimpleXML extension will do that.

And lastly JSON doesn't really carry binary data at all (you cant use script tags with <[CDATA[]]>), so it's not a choice in the voice world, it really is optimized for data...

 

No no, Google uses JSON because its native to JS, and that's what most their UIs and API interfaces are written in, and another most probable reason is because it is more compact, it uses a lot less BW, which for Google is very important as well... Less bw, less processing, faster results, simple choice, really...

 

Also, and i am only saying this because i know it will draw a smile, pshht, who even uses Windows now days, that's so 2006...

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...