Using PHP script to fill in XML

2019-09-01 19:56发布

问题:

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:

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.)



回答2:

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;
?>


回答3:

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.



回答4:

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>


回答5:

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



回答6:

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;
}


回答7:

echo echo should be echo. Learn the basic syntax.



标签: php xml forms