Solved

use php variables in simple xml

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

Independent Software Vendors: 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

Suggested Solutions

Title # Comments Views Activity
ASP and Looping Thru a XML Document 4 33
Make login page safer 3 34
Set a time limit on Wordpress Cookie function. 3 16
Do not understand error message 3 26
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo‚Ķ
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

726 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