Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Convert xml field value to a php variable

Posted on 2014-10-02
Medium Priority
Last Modified: 2014-10-15
Ray Paseur has helped us with a vxml script which now successfully puts caller responses into vxml field values and several other things (ID: 40344845) -

Here is an excerpt that 'captures' the caller's input and stores it in field name "numA" -

<field name="numA" type="digits">
      <prompt>Please enter first number, followed by the pound key</prompt>
        <prompt>You entered: <value expr="numA" /> , , </prompt>

Our question is -
Is there a way to put the "numA" value above into a php variable something like $numA = <value expr="numA" />
We know this doesn't work :-)

We would like to do this as we would like to use a php query to check if the caller's input is valid and create a <prompt> to let them know if they need to try again -
We must do this verification within the same (original) vxml doc or the connection with the caller is dropped -

We have looked at simplexml, ajax, and several more obscure approaches with no success only more confusion -

Thanks in advance for any help -
Question by:rwinnick
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 22

Assisted Solution

plusone3055 earned 750 total points
ID: 40358085
This comes from

've created a parser that returns an 
object based on a xml document. 

<?xml version="1.0" encoding="ISO-8859-1" ?> 
<country name="sweden"> 
    <city name="stockholm"> 
    <city name="g�teborg"> 
<country name="usa"> 
    <city name="new york"> 

generates the following object structure: 
[country] => Array 
    [0] => stdClass Object 
            [name] => sweden 
            [city] => Array 
                    [0] => stdClass Object 
                            [name] => stockholm 
                            [user] => Array 
                                    [0] => Adam 
                                    [1] => Eve 
                    [1] => stdClass Object 
                            [name] => g�teborg 
                            [user] => God 
    [1] => stdClass Object 
            [name] => usa 
            [city] => stdClass 
                    [name] => new york 
                    [user] => Array 
                            [0] => Clinton 
                            [1] => Bush 

Here is the code: 

class XMLParser { 
    var $path; 
    var $result; 

    function XMLParser($encoding, $data) { 
        $this->path = "\$this->result"; 
        $this->index = 0; 
        $xml_parser = xml_parser_create($encoding); 
        xml_set_object($xml_parser, &$this); 
        xml_set_element_handler($xml_parser, 'startElement', 'endElement'); 
        xml_set_character_data_handler($xml_parser, 'characterData'); 

        xml_parse($xml_parser, $data, true); 
        function startElement($parser, $tag, $attributeList) { 
            eval("\$vars = get_object_vars(".$this->path.");"); 
            $this->path .= "->".$tag; 
            if ($vars and array_key_exists($tag, $vars)) { 
                 eval("\$data = ".$this->path.";"); 
                     if (is_array($data)) { 
                           $index = sizeof($data); 
                           $this->path .= "[".$index."]"; 
                     } else if (is_object($data)) { 
                           eval($this->path." = array(".$this->path.");"); 
                           $this->path .= "[1]"; 
            eval($this->path." = null;"); 

            foreach($attributeList as $name => $value) 
                eval($this->path."->".$name. " = '".XMLParser::cleanString($value)."';"); 
    function endElement($parser, $tag) { 
        $this->path = substr($this->path, 0, strrpos($this->path, "->")); 
    function characterData($parser, $data) { 
        eval($this->path." = '".trim($data)."';"); 

Open in new window

LVL 111

Accepted Solution

Ray Paseur earned 750 total points
ID: 40358199
I'm not seeing a way to do this in PHP without a research project.  Let me try to explain what I think is going on.

The XML document gets created and sent to the VXML engine, where it is run as a set of instructions.  You basically have three possible ways to communicate with a PHP server.  You have <submit> as shown below, and there are two other tags, <subdialog> and <data>.  As I understand it the latter is semi-experimental.  We used <submit> to capture the keystroke information that the caller entered.  And we recorded that information inside the PHP script.  But the PHP script was run while the VXML script was running, and there was nothing I could find that would allow us to modify the VXML script in "real time."  VXML would not wait for the PHP script to finish and respond.   I believe that <subdialog> can provide for conditional execution.

Here was our earlier script that used <submit>...
<?xml version="1.0"?>
<vxml version="2.1">
    <field name="numA" type="digits">
      <prompt>Please enter first number, followed by the pound</prompt>
        <prompt>You entered: <value expr="numA" /> , , </prompt>
    <field name="numB" type="digits">
      <prompt>Please enter second number, followed by the pound</prompt>
        <prompt>You entered: <value expr="numB" /> , , Thank you , , Goodbye</prompt>
        <submit next="" method="post" namelist="numA numB" />

Open in new window

An alternative to PHP may be to perform the validation in JavaScript.  There is a CDATA statement that can be used inside a <script> tag.  The CDATA can contain JavaScript statements that can operate on the input numbers and can inject the results of the JavaScript back into the VXML dialog.  I'm not sure if this is the exact script, but I know I got a "factorial" script to work during our earlier testing.
$vxml = <<<EOD
<?xml version="1.0" ?>
<vxml version="2.0" xmlns="">

      function factorial(n) {return (n <= 1) ? 1 : n * factorial(n-1);}

  <!-- Primary VoiceXML form for the application -->
  <form id="computefactorial">

    <!-- variable to hold result -->
    <var name="result"/>

      <!-- The field element holds 1 piece of info gotten from the caller. -->
     <field name="num" type="digits">

      <!-- Ask the caller for the information. -->
      <prompt>Please say a number </prompt>

      <!-- The filled tag is run when the interpreter -->
      <!-- gets a valid answer from the caller -->

        <!-- Compute the factorial and assign it to the result variable -->
          expr="factorial(num)" />
        <!-- Tell the caller the answer -->
        <prompt>The factorial of
          <value expr="num"/> is
          <value expr="result"/>

      <!-- Close everything off. -->

Open in new window

So my guess is that the best line of pursuit would be to write a JavaScript function to validate the callers' inputs.  If you're using PHP to generate the VXML document, you may want to consider using PHP to generate the JavaScript in the CDATA block inside the <script> tag.  PHP HEREDOC notation is very useful when you have to use the PHP scripting language to generate statements in another scripting language.

HTH, ~Ray

Author Closing Comment

ID: 40383458
Thanks for the suggestions -
I'm going to close this question as it appears that some serious study on my part will be involved in addressing this problem -
I'm splitting the points and giving this go 'round a grade of 'B' as we really didn't solve the problem but the suggestions have given me a good starting point -
I will follow up on all the suggestions and see if I can get a handle on how one might accomplish/resolve the question -
Sometimes you just have to chase things for awhile -
I always appreciate EE's inputs as they continue to add to my education -
Thanks again
LVL 111

Expert Comment

by:Ray Paseur
ID: 40383505
Good luck, ~Ray

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question