Solved

use php variables in simple xml

Posted on 2014-09-12
4
398 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 110

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

691 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