?
Solved

use php variables in simple xml

Posted on 2014-09-12
4
Medium Priority
?
407 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 1000 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 111

Accepted Solution

by:
Ray Paseur earned 1000 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

WordPress Tutorial 3: Plugins, Themes, and Widgets

The three most common changes you will make to your website involve the look (themes), the functionality (plugins), and modular elements (widgets).

In this article we will briefly define each again, and give you directions on how to install them.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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 dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

762 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