Solved

use php variables in simple xml

Posted on 2014-09-12
4
381 Views
Last Modified: 2014-09-12
We are just learning vxml and are trying to use $_REQUEST to get a variable from the url and place it in a dom doc to create a simple xml/vxml output -

We have tried the following code but get a parse error -
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in xxxxxxxxxxxxxx.php on line 10 -

What are we missing -
Thanks in advance for the help

<?php
$doc = new DomDocument('1.0');
 $doc ="<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
 $doc .="<vxml version = \"2.1\" >\n";
$doc .="<form id=\"mainDialog\">\n";
$doc .="<prompt> your caller id is <?php $_REQUEST['callerid'];?></prompt>\n";      
$doc .="</form>\n";
$doc .="</vxml>\n";
$sxe = new SimpleXMLElement($doc);
    echo $sxe->asXML();
?>
0
Comment
Question by:rwinnick
  • 2
4 Comments
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 40319620
You'll need to concatenate the request:
$doc .="<prompt> your caller id is " . $_REQUEST['callerid'] . "</prompt>\n"; 

Open in new window

0
 
LVL 43

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 250 total points
ID: 40319636
An alternative is to use the HereDoc approach. Doing this is often easier to debug because you don't have to escape everything. If you do it this way, you'll need to wrap your $_REQUEST in curly brackets:

$doc = new DomDocument('1.0');
$doc = <<<EOT
<?xml version="1.0" encoding="utf-8"?>
<vxml version = "2.1" >
	<form id="mainDialog">
		<prompt> your caller id is {$_REQUEST['callerid']}</prompt>      
	</form>
</vxml>
EOT;

Open in new window

0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 250 total points
ID: 40319685
+1 for HEREDOC.  It makes everything easier.  Heed, but do not be put off, by the warning.
http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

There does not appear to be any need for DOMDocument or SimpleXMLElement here.
Example: http://iconoun.com/demo/temp_rwinnick.php?callerid=ABC

<?php // demo/temp_rwinnick.php
error_reporting(E_ALL);

// USE THE REQUEST VARIABLE OR A DEFAULT VALUE
$cid = !empty($_REQUEST['callerid']) ? $_REQUEST['callerid'] : "MISSING";

// CREATE THE XML STRING USING HEREDOC NOTATION
$doc = <<<EOD
<?xml version="1.0" encoding="utf-8"?>
<vxml version = "2.1" >
<form id="mainDialog">
<prompt>Your caller id is $cid</prompt>
</form>
</vxml>
EOD;

// WRITE THE XML TO THE BROWSER
echo $doc;

Open in new window

0
 

Author Closing Comment

by:rwinnick
ID: 40319775
Chris provided initial insight and Ray elaborated in depth so owe both you guys our thanks and feel it's fair to split the points evenly -
Hope this is OK with all -
As always EE is the greatest -
Thanks
Richard
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

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…
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…
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 create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

785 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