无法使用 simplexml_load_string 从 XML 响应访问属性
Can't access attributes from XML response using simplexml_load_string
我正在尝试使用 simplexml_load_string 从以下 XML 响应中获取状态和文本值;
<?xml version="1.0" encoding="utf-16"?>
<cXML payloadID="online" xml:lang="en" timestamp="2017-12-04T15:57:47.6693296+00:00">
<Response>
<Status code="402" text="	
 product 325552not in customer[20690] pricelist" />
</Response>
</cXML>
在我的 PHP 代码中,我从 $reply:
得到上面的 XML
$reply = curl_exec($curl);
然后我像这样使用 simplexml_load_string:
$responseData = simplexml_load_string($reply);
echo 'Sync Order - '. $order->getIncrementId() . ' Status '. $responseData->Response->Status['code'] .' - '. $responseData->Response->Status['text'];
但这似乎没有从上面的 XML 响应中获取代码和文本。想知道是否有人有任何想法可以提供帮助?
谢谢。
注意:cXML是正确的。
我尝试添加了 xml header 和 UTF-16 编码位,但加载失败并出现错误...
PHP Warning: simplexml_load_string(): Entity: line 1: parser error :
Document labelled UTF-16 but has UTF-8 content in
一个简单粗暴的方法是将 xml 元素中的 UTF 更改为 UTF8...
$reply = preg_replace('/(<\?xml[^?]+?)utf-16/i', 'utf-8', $reply);
$responseData = simplexml_load_string($reply);
然后给出预期的输出...
Status 402 -
product 325552not in customer[20690] pricelist
我正在尝试使用 simplexml_load_string 从以下 XML 响应中获取状态和文本值;
<?xml version="1.0" encoding="utf-16"?>
<cXML payloadID="online" xml:lang="en" timestamp="2017-12-04T15:57:47.6693296+00:00">
<Response>
<Status code="402" text="	
 product 325552not in customer[20690] pricelist" />
</Response>
</cXML>
在我的 PHP 代码中,我从 $reply:
得到上面的 XML$reply = curl_exec($curl);
然后我像这样使用 simplexml_load_string:
$responseData = simplexml_load_string($reply);
echo 'Sync Order - '. $order->getIncrementId() . ' Status '. $responseData->Response->Status['code'] .' - '. $responseData->Response->Status['text'];
但这似乎没有从上面的 XML 响应中获取代码和文本。想知道是否有人有任何想法可以提供帮助?
谢谢。
注意:cXML是正确的。
我尝试添加了 xml header 和 UTF-16 编码位,但加载失败并出现错误...
PHP Warning: simplexml_load_string(): Entity: line 1: parser error : Document labelled UTF-16 but has UTF-8 content in
一个简单粗暴的方法是将 xml 元素中的 UTF 更改为 UTF8...
$reply = preg_replace('/(<\?xml[^?]+?)utf-16/i', 'utf-8', $reply);
$responseData = simplexml_load_string($reply);
然后给出预期的输出...
Status 402 -
product 325552not in customer[20690] pricelist