繁体   English   中英

从PHP SOAP响应解析XML数据

[英]Parse XML data from PHP SOAP Response

在Brian Driscoll的帮助下,我已经成功地从正在使用的API中获得响应,但是在解析该XML数据时遇到了麻烦。 这是获取响应和返回的XML数据的脚本-

$clientC = new SoapClient('http://webservice.nada.com/vehicles/vehicle.asmx?wsdl',array('trace' => 1,'exceptions' => 1, 'cache_wsdl' => 0));

$params = new stdClass();
$params->Token = $token;
$params->Period = 1;
$params->VehicleType = "UsedCar";
$params->Vin = '5YFBURHE3FP331896';
$params->Region = 10;
$params->Mileage = 100;

$result = $clientC->getDefaultVehicleAndValueByVin(array('vehicleRequest' => $params));

$xml = htmlspecialchars($clientC->__getLastResponse());

这将返回-

<?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <soap:Body>
            <getDefaultVehicleAndValueByVinResponse xmlns="http://webservice.nada.com/">
                <getDefaultVehicleAndValueByVinResult>
                    <Uid>1182699</Uid>
                    <VehicleYear>2015</VehicleYear>
                    <MakeCode>47</MakeCode>
               </getDefaultVehicleAndValueByVinResult> 
           </getDefaultVehicleAndValueByVinResponse>
      </soap:Body>
    </soap:Envelope>

我想解析XML并检索类似

$vehicle_year = $xml->VehicleYear;

我努力了 -

$xml = htmlspecialchars($clientC->__getLastResponse());

$response = strtr($xml, ['</soap:' => '</', '<soap:' => '<']);
$output = json_decode(json_encode(simplexml_load_string($response)));
var_dump($output->Body->getHighVehicleAndValueByVinResponse->VehicleYear);

但返回NULL

这是需要浏览的各种名称空间的常见问题。 根节点定义了xmlns:soap因此您无需执行任何操作即可使用它,因此XPath使用//soap:Body/*在body标签内查找元素,因为xpath()返回匹配节点列表,请使用[0]仅选择一个。

由于主体数据全部位于默认名称空间(定义为xmlns="http://webservice.nada.com/" )下,因此可以使用$data->children("http://webservice.nada.com/") xmlns="http://webservice.nada.com/"提取所有$data->children("http://webservice.nada.com/") 现在,您可以使用标准对象符号来访问值。

需要注意的一件事是,尽管echo自动将其转换为字符串,但是如果您在其他地方使用这些值-您可能需要使用(string)对其进行转换,因为该项目实际上是SimpleXMLElement。

$data = <<< XML
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <getDefaultVehicleAndValueByVinResponse xmlns="http://webservice.nada.com/">
            <getDefaultVehicleAndValueByVinResult>
            <Uid>1182699</Uid>
            <VehicleYear>2015</VehicleYear>
            <MakeCode>47</MakeCode>
            </getDefaultVehicleAndValueByVinResult> 
       </getDefaultVehicleAndValueByVinResponse>
    </soap:Body>
</soap:Envelope>
XML;

$xml = simplexml_load_string($data);
$data = $xml->xpath("//soap:Body/*")[0];
$details = $data->children("http://webservice.nada.com/");
echo (string)$details->getDefaultVehicleAndValueByVinResult->VehicleYear;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM