我创建了一个字符串
<?xml version='1.0' encoding='ISO-8859-1'?>
<response>
<content>Question - aa.Reply the option corresponding to your answer(You can vote only once)</content>
<options>
<option url="http://localhost/poll/index.php?message=vote%3Aaar1%3Asdy&mobile=9747444565" name="sdy"/>
<option url="http://localhost/poll/index.php?message=vote%3Aaar1%3Ab&mobile=9747444565" name="b"/>
</options>
</response>
选项标记的url属性是从以下php代码
$appUrl."/poll/index.php?message=".urlencode("vote:".$kwd.":".$oopt)."&mobile=".urlencode($_GET['mobile']);
但是当我将其转换为xml时,我收到以下错误。
此页面包含以下错误:
第240行第1行的错误:EntityRef:expecting';' 下面是第一个错误的页面呈现。
为什么会发生这种情况。我确信它是url编码的问题。那么url编码的正确方法是什么。我的意思是 应该对url编码应用哪些更改
$appUrl."/poll/index.php?message=".urlencode("vote:".$kwd.":".$oopt)."&mobile=".urlencode($_GET['mobile']);
获取参数及其值为
$_GET['message'] = "vote:".$kwd.":".$oopt
$_GET['mobile'] = 888888errt434
答案 0 :(得分:2)
您在网址中有未编码的&
(&符号)字符。 &
是所有基于SGML的标记形式中的特殊字符。
htmlspecialchars()
将解决问题:
htmlspecialchars($appUrl."/poll/index.php?message=".urlencode("vote:".$kwd.":".$oopt)."&mobile=".urlencode($_GET['mobile']));
我个人更喜欢使用DOM来创建XML文档而不是字符串连接。这也将正确处理SGML特殊字符的编码。我会做这样的事情:
// Create the document
$dom = new DOMDocument('1.0', 'iso-8859-1');
// Create the root node
$rootEl = $dom->appendChild($dom->createElement('response'));
// Create content node
$content = 'Question - aa.Reply the option corresponding to your answer (You can vote only once)';
$rootEl->appendChild($dom->createElement('content', $content));
// Create options container
$optsEl = $rootEl->appendChild($dom->createElement('options'));
// Add the options - data from wherever you currently get it from, this array is
// just meant as an example of the mechanism
$options = array(
'sdy' => 'http://localhost/poll/index.php?message=vote%3Aaar1%3Asdy&mobile=9747444565',
'b' => 'http://localhost/poll/index.php?message=vote%3Aaar1%3Ab&mobile=9747444565'
);
foreach ($options as $name => $url) {
$optEl = $optsEl->appendChild($dom->createElement('option'));
$optEl->setAttribute('name', $name);
$optEl->setAttribute('url', $url);
}
// Save document to a string (you could use the save() method to write it
// to a file instead)
$xml = $dom->saveXML();