Solved

Convert xml field value to a php variable

Posted on 2014-10-02
4
261 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 109

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 109

Expert Comment

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

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Wordpress errors showing up, hacked? 4 33
php extract($_REQUEST) 5 47
ebay api to see some data about my customers 2 28
Showing random records from database 10 36
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

776 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