array(2) {
["header"]=>
string(267) "HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=BDED717E8585DDA55A47EA1822C00E80; Path=/sdkproxy/; HttpOnly
Content-Type: text/html;charset=UTF-8
Content-Language: en-GB
Content-Length: 103
Date: Sat, 24 Aug 2013 02:38:00 GMT
Connection: close"
["body"]=>
string(103) "
<?xml version="1.0" encoding="UTF-8"?><response><error>0</error><message>-1100.0</message></response>"
}
是一个web服务端的返回值
用 simplexml_load_string解析返回false,我都诧异了['body']的内容如何转换成一个数组形式
["header"]=>
string(267) "HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=BDED717E8585DDA55A47EA1822C00E80; Path=/sdkproxy/; HttpOnly
Content-Type: text/html;charset=UTF-8
Content-Language: en-GB
Content-Length: 103
Date: Sat, 24 Aug 2013 02:38:00 GMT
Connection: close"
["body"]=>
string(103) "
<?xml version="1.0" encoding="UTF-8"?><response><error>0</error><message>-1100.0</message></response>"
}
是一个web服务端的返回值
用 simplexml_load_string解析返回false,我都诧异了['body']的内容如何转换成一个数组形式
$xml_string = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response><error>0</error><message>-1100.0</message></response>";
$xml = simplexml_load_string($xml_string);
//对象转换成数组,方便引用
$arr = (array)$xml;
var_dump($arr);
$xml = simplexml_load_string($s);
print_r($xml);
SimpleXMLElement Object
(
[error] => 0
[message] => -1100.0
)
问题是我这里就是返回bool(false)
比如你接受的的数据在 $data 中,则
echo serialize($data);
贴出结果
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=96B4D5E802AEBBE6A1F92824C576690B; Path=/sdkproxy/; HttpOnly
Content-Type: text/html;charset=UTF-8
Content-Language: en-GB
Content-Length: 103
Date: Sat, 24 Aug 2013 03:22:14 GMT
Connection: close";s:4:"body";s:103:"
<?xml version="1.0" encoding="UTF-8"?><response><error>0</error><message>-1100.0</message></response>";}我感觉是数据的问题
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=96B4D5E802AEBBE6A1F92824C576690B; Path=/sdkproxy/; HttpOnly
Content-Type: text/html;charset=UTF-8
Content-Language: en-GB
Content-Length: 103
Date: Sat, 24 Aug 2013 03:22:14 GMT
Connection: close";s:4:"body";s:103:"
<?xml version="1.0" encoding="UTF-8"?><response><error>0</error><message>-1100.0</message></response>";}';
$d = unserialize($s);
$xml = simplexml_load_string(trim($d['body']));
print_r($xml);SimpleXMLElement Object
(
[error] => 0
[message] => -1100.0
)
你在获取数据时,没有切割干净。
http 数据头和数据体之间是“\r\n\r\n”,而你是按“\r\n”切割的
所以在数据体前面留有的“\r\n”造成分析失败