重新格式化 PHP var_dump

Reformatting a PHP var_dump

我刚刚使用 SoapClient 连接到 Web 服务。基于特定输入(车辆的 VIN)的有关车辆的网络服务 returns 信息。我的 $result 变量设置为等于网络服务输出;因此,var_dump ($result); 转储所有车辆信息。我很难解码它。这是我收到的奥迪 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 "
    }
}

如何重新格式化输出?我想从中拉动弦乐。例如,我想重新格式化示例输出,使其显示如下:

响应:成功
世界制造商 ID:德国 Audi Nsu

这是我的 PHP 代码(我省略了网络服务的用户名和密码):

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

?>

___________________________________________________________________________

又遇到了障碍。似乎我越深入地研究数据,创建新变量的调用数据就越复杂。我正在尝试提取 "Audi S4"、“5 Door Wagon”和 "All-Wheel Drive"

这是我正在努力解决的部分:

["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) ""
  }
}

___________________________________________________________________________

通过添加:

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

我能够在 XML 中获得输出。这是我试图从中提取以形成变量的数据:

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

使用 var_dump 通常可以让程序员轻松测试某些值的结果,就像您所做的那样。但是,在您的情况下,如果您非常确定 return 数据结构 的结果,我建议您通过直接打印出所需数据来重新格式化结果:

错误的值访问演示脚本:

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

编辑后的正确脚本在这里:

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

对于最新的 XML 部分,您必须在 XML 结果周围 包裹 额外标签,然后再手动加载它。使用 getElementsByTagName() and getAttribute 允许我使用循环函数加载结果:

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

尝试回显其上方的 html pre 元素,它应该如下所示:

<?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
]);

echo "<pre>";
var_dump ($result);
echo "</pre>"; // optional       
?>

或者,使用 print_r()