Using PHP script to fill XML
we are all familiar with "injecting" PHP scripts into HTML pages to perform tasks such as displaying form results, but how can this be done to render 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 says parsing "unexpected T_STRING on line 1" . Any help or tutorials would be appreciated.
a source to share
There isn't much difference between XML and HTML templates.
<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 <?
their own, not <?php
) as PHP code, then the XML declaration will fix it. This was the reason for your error.
While you can fix this problem by disabling short tags, your best bet is to remove the declaration <?xml
instead. It doesn't make sense to include the XML declaration in the XML file anyway (unless you are using XML 1.1 or a character encoding that is not UTF-8, which should usually be avoided).
As in HTML, you need to avoid the <
and characters &
(and quotes in attribute values), otherwise your markup will be broken. htmlspecialchars
works as well for XML as it does for HTML. (You can define a function with a shorter name to do echo htmlspecialchars
to shorten the input.)
a source to share
You can change the first line to:
<?php echo '<?xml...'; ?>
to fix the problem, you could also disable the short shorts if you like.
You can 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;
?>
a source to share
You don't need to have a space between the first question mark and xml
: PHP sees <?
and assumes that you intended to shorten it for <?php
.
See a few paragraphs in this PHP Implementation Tutorial :
An XML processing directive is a command embedded in an XML document to process an application. All XML processing directives take the form:
<? app name for the app? >
Note that the statement is opened and closed with nested parentheses and question marks, and that the application link name must appear immediately after the first question mark without a space before it . The information for the application can be a brief instruction, or, in the case of PHP, it can continue for pages and pages.
The accent was mine.
a source to share
Use native XML PHP classes ( http://www.php.net/manual/en/book.xmlwriter.php ) which will also provide you the data you need:
<?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 <properly encoded?> dog.</Msg>
</Response>
a source to share