重新格式化PHP var_dump

时间:2018-09-13 02:20:59

标签: php soap soap-client var-dump

我刚刚使用SoapClient连接到Web服务。 Web服务基于特定的输入(车辆的VIN)返回有关车辆的信息。我的DWARF变量设置为等于Web服务输出;因此,$result转储了所有车辆信息。我在解码时遇到困难。这是我收到的奥迪VIN输出的一部分:

var_dump ($result);

如何重新格式化输出?我想从中拉弦。例如,我想重新格式化示例输出,使其看起来像这样:

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

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

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

___________________________________________________________________________

遇到另一个障碍。看来,我越深入研究数据,创建新变量的调用数据就越复杂。我正在尝试提取“奥迪S4”,“ 5门旅行车”和“全轮驱动”

这是我苦苦挣扎的部分:

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

?>

___________________________________________________________________________

通过添加:

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

我能够获取XML格式的输出。这是我尝试从中获取以形成变量的数据:

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

2 个答案:

答案 0 :(得分:1)

对于程序员来说,使用VERBOSE=1通常很容易测试某些值的结果,例如您所做的。但是,如果您非常确定返回数据结构的结果,建议您直接打印所需数据来重新格式化结果:

错误的价值获取演示脚本:

var_dump

已编辑的正确脚本在此处:

<?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()getAttribute允许我使用循环功能加载结果:

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

答案 1 :(得分:0)

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

---------------------------------------
Header Content Duplicate ... some words
---------------------------------------

      CONTENT

--------------------------------------
Footer Content  Duplicate ... some words
--------------------------------------

或者,使用 print_r()