Solved

use php variables in simple xml

Posted on 2014-09-12
4
370 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 42

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 42

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 108

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

759 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

22 Experts available now in Live!

Get 1:1 Help Now