Convert xml field value to a php variable

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>
      <filled>
        <prompt>You entered: <value expr="numA" /> , , </prompt>
      </filled>
    </field>

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 -
Richard
rwinnickAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

plusone3055Commented:
This comes from
http://php.net/manual/en/function.xml-parse.php

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

example: 
<?xml version="1.0" encoding="ISO-8859-1" ?> 
<country name="sweden"> 
    <city name="stockholm"> 
        <user>Adam</user> 
        <user>Eve</user> 
    </city> 
    <city name="g�teborg"> 
        <user>God</user> 
    </city> 
</country> 
<country name="usa"> 
    <city name="new york"> 
        <user>Clinton</user> 
        <user>Bush</user> 
    </city> 
</country> 

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); 
        xml_parser_free($xml_parser); 
    } 
    
        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

0
Ray PaseurCommented:
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">
  <form>
    <field name="numA" type="digits">
      <prompt>Please enter first number, followed by the pound</prompt>
      <filled>
        <prompt>You entered: <value expr="numA" /> , , </prompt>
      </filled>
    </field>
    <field name="numB" type="digits">
      <prompt>Please enter second number, followed by the pound</prompt>
      <filled>
        <prompt>You entered: <value expr="numB" /> , , Thank you , , Goodbye</prompt>
        <submit next="http://iconoun.com/demo/temp_rwinnick_writefile.php" method="post" namelist="numA numB" />
      </filled>
    </field>
  </form>
</vxml>

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="http://www.w3.org/2001/vxml">

  <!-- COMPUTE FACTORIAL -->
  <script>
    <![CDATA[
      function factorial(n) {return (n <= 1) ? 1 : n * factorial(n-1);}
    ]]>
  </script>

  <!-- 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 -->
      <filled>

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

      <!-- Close everything off. -->
      </filled>
    </field>
  </form>
</vxml>

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
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rwinnickAuthor Commented:
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
Richard
0
Ray PaseurCommented:
Good luck, ~Ray
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.