简体   繁体   中英

Parse XML data from PHP SOAP Response

I have successfully been able to get a response from an API im working with, with the help of Brian Driscoll but am having trouble parsing that XML data. Here is the script to get the response and the returned XML data -

$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());

This returns -

<?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>

I would like to either parse the XML and retrieve something like

$vehicle_year = $xml->VehicleYear;

I have tried -

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

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

But returns NULL

This is the usual problem of having various namespaces which you need to navigate around. The root node defines xmlns:soap so you can use that without having to do anything, so the XPath uses //soap:Body/* to find the element inside the body tag, as xpath() returns a list of matching nodes, use [0] to just pick the only one out.

As the body data is all under a default namespace (defined as xmlns="http://webservice.nada.com/" ) you can extract all of them using $data->children("http://webservice.nada.com/") . This now allows you to use standard object notation to access the values.

One thing to note is although echo automatically converts it to a string, if you use these values elsewhere - you may need to convert it using (string) as the item is in fact a 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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