开发者

cURL Response in XML - Simplexml_load_string failed

I'm making a cURL request into an authenticated area which is working fine; and when echoing out the response everything looks good except for the first part. This is my exact response (with the otherData tag replacing my actual data).

 <?xml version='1.0' encoding='UTF-8'?>
    <Response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <ResponseStatus>
           <StatusCode>0</StatusCode>
           <StatusMessage>Success</StatusMessage>
        </ResponseStatus>
        <otherData></otherData>
    </Response>

See how the xml version='1.0' encoding='utf-8' and then we switch to using " in the actual 开发者_开发知识库XML? When I'm trying to load this with simplexml_load_string it fails due to that. I know I could simply do a str_replace, however that isn't really understanding the full issue here.


Can we see your php code? I'm running:

<?php
    $xml_string =<<<XML
<?xml version='1.0' encoding='UTF-8'?>
<Response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <ResponseStatus>
       <StatusCode>0</StatusCode>
       <StatusMessage>Success</StatusMessage>
    </ResponseStatus>
    <otherData></otherData>
</Response>
XML;

$xml_obj = simplexml_load_string($xml_string);
echo $xml_obj->ResponseStatus->StatusMessage."\n";
?>

And it seems to work fine.


try replacing all " with '.

$string = str_replace("\"", "'", $string);

The string is being closed at the double quotes.

So the string is printing as

$string = "<?xml version='1.0' encoding='UTF-8'?><Response xmlns:xsi=" so obviously using str_replace() would turn that " it's closing on into a ' making the string correctly output.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜