0

I need to send xml in POST body (it must contain ONLY xml text). I use code like this, but I don't know how to escape characters like '<?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 );

And print_r( $_POST ) in server side return this incorrect code:

(
    [<?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>
)
john BB
  • 79
  • 9
  • As you're developing, it's of great use to listen to any info, warning and even error PHP has to tell you. That often leads to a more direct solution for a then more concrete programming question as an exact error message is given. One reference question we have on site for some PHP debugging techniques is [How to get useful error messages in PHP?](http://stackoverflow.com/q/845021/367456) – hakre Oct 31 '15 at 15:58

1 Answers1

2

You must not use the same type of quotes within quotes without escaping them:

Wrong:

"<?xml version="1.0" encoding="WINDOWS-1251"?>"

Should be:

"<?xml version=\"1.0\" encoding=\"WINDOWS-1251\"?>"

or just replace the outer double quotes with single quotes:

'<?xml version="1.0" encoding="WINDOWS-1251"?>'
smat88dd
  • 2,258
  • 2
  • 25
  • 38