我需要在POST主体中发送xml(它必须只包含xml文本)。
我使用这样的代码,但我不知道如何转义'<?xml'
,'?>'
等字符。
<?php
$url = 'http://' . $_SERVER[ 'SERVER_NAME' ] . '/api/setExchangeRate/';
$xmlcontent = "<?xml version="1.0" encoding="WINDOWS-1251"?>" .
'<Data>
<Off UID="0001">
<SetupDateTime>DD MM YYYY HH:MM</SetupDateTime>
<Currency CODE="840">
<Buy>67.50</Buy>
<Sell>67.50</Sell>
</Currency>
<Currency CODE="978">
<Buy>75.40</Buy>
<Sell>77.10</Sell>
</Currency>
</Off>
</Data>';
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_URL, $url );
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlcontent);
$content=curl_exec($ch);
print_r( $content );
服务器端的print_r( $_POST )
返回错误的代码:
(
[<?xml_version] => "1.0" encoding="WINDOWS-1251"?><Data>
<Off UID="0001">
<SetupDateTime>DD MM YYYY HH:MM</SetupDateTime>
<Currency CODE="840">
<Buy>67.50</Buy>
<Sell>67.50</Sell>
</Currency>
<Currency CODE="978">
<Buy>75.40</Buy>
<Sell>77.10</Sell>
</Currency>
</Off>
</Data>
)
答案 0 :(得分:1)
在不转义引号的情况下,不得在引号内使用相同类型的引号:
错:
"<?xml version="1.0" encoding="WINDOWS-1251"?>"
应该是:
"<?xml version=\"1.0\" encoding=\"WINDOWS-1251\"?>"
或者只用单引号替换外部双引号:
'<?xml version="1.0" encoding="WINDOWS-1251"?>'