Solved

Convert xml field value to a php variable

Posted on 2014-10-02
4
257 Views
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>
      <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
0
Comment
Question by:rwinnick
  • 2
4 Comments
 
LVL 22

Assisted Solution

by:plusone3055
plusone3055 earned 250 total points
ID: 40358085
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
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 250 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">
  <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
 

Author Closing Comment

by:rwinnick
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
Richard
0
 
LVL 108

Expert Comment

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Time difference 10 35
How can I expand text with explaination 5 22
PHP string issue 5 18
File not deleting using unlink 6 13
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now