Search code examples
phpsoapsoap-clientvar-dump

Reformatting a PHP var_dump


I just connected to a web service using SoapClient. The web service returns information regarding a vehicle based on a specific input, the vehicle's VIN. My $result variable is set equal to the web service output; thus, var_dump ($result); dumps all the vehicle information. I am having difficulty decoding it. Here is a portion of the output I received for an Audi VIN:

object(stdClass)#2 (14) {
    ["responseStatus"]=> object(stdClass)#3 (2) {
        ["responseCode"]=> string(10) "Successful"
        ["description"]=> string(10) "Successful"
    }
    ["vinDescription"]=> object(stdClass)#4 (11) {
        ["WorldManufacturerIdentifier"]=> string(17) "Germany Audi Nsu "
    }
}

How can I reformat the output? I want to pull the strings from it. For example, I would like to reformat the example output so that it reads like this:

Response: Successful
World Manufacturer Id: Germany Audi Nsu

Here is my PHP code (I have omitted my username and password to the web service):

<?php
$client = new SoapClient('http://services.chromedata.com/Description/7b?wsdl');
$account = ['number'=>"", 'secret'=>"", 'country'=>"US",    'language'=>"en"];
$vin = $_POST["b12"];

$result = $client->describeVehicle([
'accountInfo' => $account,
'vin' => $vin
]);



var_dump ($result);

?>

___________________________________________________________________________

Encountered another roadblock. It seems the further I dig into the data the more complex calling data to create new variables becomes. I am attempting to extract "Audi S4", "5 Door Wagon", and "All-Wheel Drive"

Here is the section I am struggling with:

["technicalSpecification"]=>
array(97) {
 [0]=>
 object(stdClass)#640 (2) {
  ["titleId"]=>
  int(1)
  ["value"]=>
  array(2) {
    [0]=>
    object(stdClass)#641 (3) {
      ["styleId"]=>
      array(2) {
        [0]=>
        int(292015)
        [1]=>
        int(292016)
      }
      ["value"]=>
      string(7) "Audi S4"
      ["condition"]=>
      string(3) "-PT"
    }
    [1]=>
    object(stdClass)#642 (3) {
      ["styleId"]=>
      array(2) {
        [0]=>
        int(292015)
        [1]=>
        int(292016)
      }
      ["value"]=>
      string(7) "Audi S4"
      ["condition"]=>
      string(0) ""
    }
  }
}
[1]=>
object(stdClass)#643 (2) {
  ["titleId"]=>
  int(2)
  ["value"]=>
  object(stdClass)#644 (3) {
    ["styleId"]=>
    array(2) {
      [0]=>
      int(292015)
      [1]=>
      int(292016)
    }
    ["value"]=>
    string(12) "5 Door Wagon"
    ["condition"]=>
    string(0) ""
  }
}
[2]=>
object(stdClass)#645 (2) {
  ["titleId"]=>
  int(6)
  ["value"]=>
  object(stdClass)#646 (3) {
    ["styleId"]=>
    array(2) {
      [0]=>
      int(292015)
      [1]=>
      int(292016)
    }
    ["value"]=>
    string(15) "All-Wheel Drive"
    ["condition"]=>
    string(0) ""
  }
}

___________________________________________________________________________

By adding:

$resultxml = htmlentities($client->__getLastResponse()) . "\n";
echo $resultxml;

I am able to get the output in XML. Here is the data I am trying to pull from to form variables with:

 <technicalSpecification>
        <titleId>1</titleId>
        <value value="Audi S4" condition="-PT">
           <styleId>292015</styleId>
           <styleId>292016</styleId>
        </value>
        <value value="Audi S4" condition="">
           <styleId>292015</styleId>
           <styleId>292016</styleId>
        </value>
     </technicalSpecification>
     <technicalSpecification>
        <titleId>2</titleId>
        <value value="5 Door Wagon" condition="">
           <styleId>292015</styleId>
           <styleId>292016</styleId>
        </value>
     </technicalSpecification>
     <technicalSpecification>
        <titleId>6</titleId>
        <value value="All-Wheel Drive" condition="">
           <styleId>292015</styleId>
           <styleId>292016</styleId>
        </value>
     </technicalSpecification>

Solution

  • Using var_dump is usually ease for the programmer to test out the outcome of certain values like what you did. However in your case, if you are very sure the outcome of the return data structure, I suggest you to reformat the outcome by straight printing out the desired data:

    Wrong value-accessing demonstration script :

    <?php
    $client = new SoapClient('http://services.chromedata.com/Description/7b?wsdl');
    $account = ['number'=>"", 'secret'=>"", 'country'=>"US",    'language'=>"en"];
    $vin = $_POST["b12"];
    
    $result = $client->describeVehicle([
    'accountInfo' => $account,
    'vin' => $vin
    ]);
    
    if(property_exists($result, 'responseCode') && property_exists($result,'WorldManufacturerIdentifier')){
        echo "Response : ".$result['responseStatus']['responseCode']."<br>World Manufacturer Id : ".$result['vinDescription']['WorldManufacturerIdentifier']."<br>";
    }else{
        echo "The returned data is not complete!"; //or other proper error messages
    }
    ?>
    

    The edited correct script is here :

    <?php
    $client = new SoapClient('http://services.chromedata.com/Description/7b?wsdl');
    $account = ['number'=>"", 'secret'=>"", 'country'=>"US",    'language'=>"en"];
    $vin = $_POST["b12"];
    
    $result = $client->describeVehicle([
    'accountInfo' => $account,
    'vin' => $vin
    ]);
    
    if(property_exists($result, 'responseCode') && 
    property_exists($result,'WorldManufacturerIdentifier')){
        echo "Response : ".$result->responseStatus->responseCode."<br>World Manufacturer Id : ".$result->vinDescription->WorldManufacturerIdentifier."<br>";
    }else{
        echo "The returned data is not complete!"; //or other proper error messages
    }
    ?>
    

    For the latest XML part, you have to wrap extra tags around your XML result before you load it manually. Using getElementsByTagName() and getAttribute allow me to load out the result with looping function:

    <?php
    $doc = new DOMDocument();
    $doc->loadXML('<some><technicalSpecification><titleId>1</titleId><value 
    value="Audi S4" condition="-PT"><styleId>292015</styleId> 
    <styleId>292016</styleId></value><value value="Audi S4" condition=""> 
    <styleId>292015</styleId><styleId>292016</styleId></value> 
    </technicalSpecification><technicalSpecification><titleId>2</titleId><value 
    value="5 Door Wagon" condition=""><styleId>292015</styleId> 
    <styleId>292016</styleId></value></technicalSpecification> 
    <technicalSpecification><titleId>6</titleId><value value="All-Wheel Drive" 
    condition=""><styleId>292015</styleId><styleId>292016</styleId></value> 
    </technicalSpecification></some>');
    $childs = $doc->getElementsByTagName("technicalSpecification");
    foreach($childs as $child){
        $value = $child->getElementsByTagName("value")[0]->getAttribute("value");
    echo $value;
    }