1

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

4
  • Why do you try to handle xml as json? PHP has a few xml extensions. Have a look at them Commented Mar 26, 2019 at 19:34
  • Thanks, Im just more familiar with JSON, havent used XML much Commented Mar 26, 2019 at 19:34
  • 1
    By the way - doesn't htmlspecialchars destroy your xml as it replaces < and >? Commented Mar 26, 2019 at 19:39
  • @kuh-chan yes you are correct, removing that was imperative! Commented Mar 26, 2019 at 19:42

1 Answer 1

3

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;
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you s much for the great explanation! This is a huge help cheers mate!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.