Solve simplexml_load_string() using simplexml_load_string

Viewed 80

After numerous attempts still no success, trying to access this 'http://www.autobid.co.za/halfway/vehicledetails.php?wsdl , providing the username and password and then gathering the xml feedback. This code :

$client = new SoapClient('http://www.autobid.co.za/halfway/vehicledetails.php?wsdl');
 $result = $client->getVehicleDetails('spwf87','hk8*9jd'); 
 echo('<pre>'.$result.'</pre>');

Dumps the entire text field, want it of course cleaned up as the page source states :

<pre><![CDATA[<VEHICLEDETAILS><VEHICLE><ID....

etc, so tried using:

$client = new SoapClient('http://www.autobid.co.za/halfway/vehicledetails.php?wsdl');

$result = $client->getVehicleDetails('pass','user');
 
$content = simplexml_load_string($result, null,LIBXML_NOCDATA);
$content = simplexml_load_string($content->pre);
$json = json_encode($content);
$content = json_decode($json,TRUE);
var_dump($content);

But still get the error of

Warning: simplexml_load_string(): Entity: line 1: parser error : StartTag: invalid element name in index.php on line 24

Warning: simplexml_load_string(): <![CDATA[<VEHICLEDETAILS><VEHICLE><ID>5002386176</ID><VIN>AHTHA3CD503427515</VIN in index.php on line 24

Warning: simplexml_load_string(): ^ in index.php on line 24

Warning: simplexml_load_string(): Entity: line 1: parser error : Extra content at the end of the document in index.php on line 24

Warning: simplexml_load_string(): <![CDATA[<VEHICLEDETAILS><VEHICLE><ID>5002386176</ID><VIN>AHTHA3CD503427515</VIN in index.php on line 24

Warning: simplexml_load_string(): ^ in index.php on line 24
bool(false)

At my wits end, have to present all the data from the source.

1 Answers

The xml parser is choking on ampersands that aren't correct entities & -> &amp;

<?php
$client = new SoapClient('http://www.autobid.co.za/halfway/vehicledetails.php?wsdl'); 
$result = $client->getVehicleDetails('spwf87','hk8*9jd');
//print_r($result);
$content = substr($result, strlen('<![CDATA['), -3);
$content=preg_replace('/&(?!#?[a-z0-9]+;)/', '&amp;', $content);
$content =  simplexml_load_string($content, 'SimpleXMLElement',LIBXML_NOCDATA);
foreach ($content->VEHICLE as $vehicle_props) {
    echo "\n\n ---------------------";
    foreach ($vehicle_props as $prop_name => $prop_value) {
        if ($prop_name == "EXTRAS")  $prop_value=implode("\n  - ", explode(",", $prop_value));
        echo "\n" . ucwords(strtolower($prop_name)) .": " . $prop_value;
    }
}
Related