<soapenv:envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:body>
<ns1:mbillcommandresponse xmlns:ns1="http://www.mysoap.com/SoapService/">
<ReturnValues>
<name>status</name>
<value>TEHNICAL_ERROR</value>
</ReturnValues>
<ReturnValues>
<name>description</name>
<value>Please contact your administrator</value>
</ReturnValues>
</ns1:mbillcommandresponse>
</soapenv:body>
</soapenv:envelope>
在CURL响应中,我得到了响应。这是我的PHP代码:
$response = curl_exec($ch);
$xml = simplexml_load_string($response);
//$result = $xml->xpath('//name'); //echo "<pre>"; print_r($result); exit;
$xml->registerXPathNamespace('ns1', 'http://www.mysoap.com/SoapService/');
foreach ($xml->xpath('//returnvalues') as $item) {
$json = json_encode($item);
$convrt_arr = json_decode($json, true);
break;
}
print_r($json); exit;
在上面的代码中,我得到了空的json。你能帮我吗?
答案 0 :(得分:2)
您的问题编辑是一个重要线索。您已将XML从小写改为CamelCase元素。更改:
CurrentTemperatureAddonm
收件人:
foreach ($xml->xpath('//returnvalues') as $item) {
,它将起作用。 Xpath查询区分大小写。
答案 1 :(得分:0)
希望这会有所帮助
$response = '<soapenv:envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:body>
<ns1:mbillcommandresponse xmlns:ns1="http://www.mysoap.com/SoapService/">
<returnvalues>
<name>status</name>
<value>TEHNICAL_ERROR</value>
</returnvalues>
<returnvalues>
<name>description</name>
<value>Please contact your administrator</value>
</returnvalues>
</ns1:mbillcommandresponse>
</soapenv:body>
</soapenv:envelope>';
$response = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $response);
$xml = new SimpleXMLElement($response);
$body = $xml->xpath('//returnvalues');
$json = json_encode((array)$body);
print_r($json);