我从我正在使用的api返回以下内容..但是使用SIMPLEXML我无法访问值:
<?xml version="1.0" encoding="utf-16"?>
<Response Version="1.0">
<DateTime>2/13/2013 10:37:24 PM</DateTime>
<Contact_ID>151-233-DD</Contact_ID>
<Quote_ID>ojc332-ewied-23e3ed</Quote_ID>
<Status>Failure</Status>
<Reason>Incorrect Contact ID</Reason>
</Response>
Run Code Online (Sandbox Code Playgroud)
我正在设置:
$variable = new SimpleXMLElement($results);
Run Code Online (Sandbox Code Playgroud)
SIMPLEXML给了我以下代替我期望的$ variable-> DateTime:
SimpleXMLElement Object ( [0] => 2/13/2013 10:37:24 PM 151-233-DD 0jc332-ewied-23e3ed Failure Incorrect Contract ID )
Run Code Online (Sandbox Code Playgroud)
任何帮助深表感谢
似乎utf-16内容具有编码类型的原因utf-8.
所以你需要改变你的编码类型,
$string = '<?xml version="1.0" encoding="utf-16"?>
<Response Version="1.0">
<DateTime>2/13/2013 10:37:24 PM</DateTime>
<Contact_ID>151-233-DD</Contact_ID>
<Quote_ID>ojc332-ewied-23e3ed</Quote_ID>
<Status>Failure</Status>
<Reason>Incorrect Contact ID</Reason>
</Response>';
$xml = simplexml_load_string(preg_replace('/(<\?xml[^?]+?)utf-16/i', '$1utf-8', $string));
Run Code Online (Sandbox Code Playgroud)
键盘演示.
也可以使用utf8_encode,
$xml = simplexml_load_string(utf8_encode($string));
Run Code Online (Sandbox Code Playgroud)