views:

114

answers:

7

Hi - we are all familiar with "embedding" PHP scripts in HTML pages to do tasks like displaying form results, but how can that be done in a way that displays XML?

For example, if I wrote:

<?xml version='1.0' ?>
<Response>
  <?php
        $body = $_GET['Body'];
        $fromPh = $_GET['From'];
        echo "<Msg>Your number is: $fromPh, and you typed: $body.</Msg>"
  ?>
</Response>

I get a message that states "parse error, unexpected T_STRING on line 1". Any help or tutorials would be appreciated.

+1  A: 

You need to not have a space between the first question mark and xml: PHP sees the <? and assumes that you intended that to be shorthand for <?php.

See a few paragraphs down in this tutorial on embedding PHP:

An XML processing directive is a command embedded in an XML document for some application to process. All XML processing directives take the form of:

<?appname information for application ?>

Notice that the statement opens and closes with nested brackets and question marks, and that the name of the application being referenced must come right after the first question mark, with no space before it. The information for the application can be a short statement, or, in the case of PHP, can go on for pages and pages.

Emphasis was mine.

Mark Rushakoff
damn beat me to it :P
Rob
This doesn't solve the problem if the php variable short_open_tag is on, as that link further describes below your quote, in the section titled "The Short Tag".
Kevin
ok, fixed the code - still looking for a solution though (same error)
Yaaqov
A: 

remove the space between <? xml so its <?xml

Rob
A: 
function genOutput() {
$output = header("Content-type: text/xml");
$output.= "<container>\n";
$output.= "<title>".$title."</title>\n";
$output.= "<text>".$text."</text>\n";
$output.="</container>\n";
return $output;
}
nik
I'm new to PHP - how would I diplay the ouput of this function when the code ran?
Yaaqov
A: 

echo echo should be echo. Learn the basic syntax.

Coronatus
+2  A: 

You could change the first line to:

<?php echo '<?xml...'; ?>

that would solve the problem, you could also disbale php shorttags if you so desired.

You could also do the following:

<?php
$body = $_GET['Body'];
$fromPh = $_GET['From'];
echo <<<XMLCODE
<?xml version='1.0' ?>
<Response>
  <msg>Your number is: $fromPh, and you typed: $body.</Msg>
</Response>
XMLCODE;
?>
NotIan
Thanks - very succint, I'll test it out.
Yaaqov
+1  A: 

There is no big difference between templating XML and HTML.

<Response>
    <Msg>
        Your number is: <?php echo htmlspecialchars($_GET['From']); ?>
        and you typed: <?php echo htmlspecialchars($_GET['Body']); ?>
    </Msg>
</Response>

If your PHP installation is configured to allow “short tags” (ie., just <? on its own rather than <?php) as being PHP code, then the XML Declaration will trip it up. This is what caused your error.

Whilst you can fix that problem by disabling short tags, it's probably best to remove the <?xml declaration instead or as well. There is no reason to include an XML Declaration in an XML file anyway (unless you're using XML 1.1, or a character encoding that isn't UTF-8, which generally you'd want to avoid).

Just as in HTML, you need to escape < and & characters (and quotes, in attribute values) otherwise your markup gets broken. htmlspecialchars works just as well for XML as it does for HTML. (You may want to define a function with a shorter name to do echo htmlspecialchars, to cut down on typing.)

bobince
There are always the arguments of strict vs loose interpretations of standards, but section 2.8 of the W3C recommendation says: 'XML documents SHOULD begin with an XML declaration which specifies the version of XML being used'. Should doesn't mean have to, but I'd not recommend omitting it simply because the first attempt made to include it didn't work.
Kevin
I'm trying out disabling short tags in .HTACCESS - thanks for contributing.
Yaaqov
I like this solution, as it is the most streamlined, and clarifies the Short Tage issue. Thanks again Bobince.
Yaaqov
+1  A: 

Use PHP's native XML classes (http://www.php.net/manual/en/book.xmlwriter.php), which also will provide the necessary data escaping etc for you:

<?php

// Your datas from somewhere
$fromPh = '867-5309';
$body = 'The quick brown fox jumped over the <properly encoded?> dog.';

$writer = new XMLWriter();

$writer->openMemory();
$writer->setIndent(true);
$writer->setIndentString(' ');
$writer->startDocument('1.0', 'UTF-8');
$writer->startElement('Response');
$writer->writeElement('Msg', 'Your number is: '.$fromPh.', and you typed: '.$body);
$writer->endElement();
$writer->endDocument();

print $writer->outputMemory();

Output:

<?xml version="1.0" encoding="UTF-8"?>
<Response>
 <Msg>Your number is: 867-5309, and you typed: The quick brown fox jumped over the &lt;properly encoded?&gt; dog.</Msg>
</Response>
Kevin
Thank you for the detailed solution - much appreciated.
Yaaqov