我正在使用PHP CURL对特定的URL进行API调用。我可以在我的浏览器中接收响应。
CURL将此响应保存在一个变量中,如下所示:
$output = curl_exec($ch);
现在,当我使用
print_r(htmlspecialchars($output, true));
我在我的浏览器中得到了这个响应。
<?xml version="1.0" encoding="utf-16" standalone="no"?>
<loginres>
<sessiontoken>123456</sessiontoken>
<timeout>7/30/2016 11:18:22 AM</timeout>
<status>1</status>
</loginres>
因为这是一个UTF-16编码的响应,所以我不能使用SimpleXML或DOM文档。你可以尝试一下(我已经失败过好几次了)。什么都不会发生。
另外,我也尝试过在Array filter中使用"explode“函数,得到的响应如下:
Array
(
[0] => version="1.0"
[2] => encoding="utf-16"
[3] => standalone="no"?>
[5] => 123456
[7] => 7/30/2016
[8] => 11:26:09
[9] => AM
[11] => 1
)
现在,我的问题是,我如何使用这个文件....我想用XML标记创建PHP变量,并对其进行解析,以便在数据库中进行操作和保存。
另一种选择是将响应保存在本地服务器的xml文件中,然后通过Jquery、Ajax使用它,但我不知道该怎么做。请帮帮忙。
发布于 2016-07-30 16:40:13
如果定义得当,DOM可以很好地加载UTF-16。API仍将返回UTF-8。
// create some xml string and convert it to utf-16
$xml = iconv('utf-8', 'utf-16', '<?xml version="1.0" encoding="utf-16"?><foo>Ä</foo>');
// look an encoded special char and a BOM
var_dump($xml);
$document = new DOMDocument();
$document->loadXml($xml);
// loaded just fine, here is the node content as UTF-8
var_dump($document->documentElement->textContent);
string(104) "��<?xml version="1.0" encoding="utf-16"?><foo>�</foo>"
string(2) "Ä"
https://stackoverflow.com/questions/38670553
复制相似问题