Codeigniter PHP - 解析SOAP(WDSL)结果

时间:2017-04-05 19:02:04

标签: php json codeigniter soap

我目前正在使用以下方法从Web服务检索信息:

$serviceData= new SoapClient('http://xxx.xxx.xxx.xx:xx/WebService/WebService.svc?wsdl');
$response = $serviceData->GetMyInformation();
var_dump($response);

var_dump的结果如下:

object(stdClass)#15 (1) 
{ ["GetDatabaseResult"]=> object(stdClass)#16 (1)
    { ["DatabaseInformation"]=> array(4) 

        {   [0]=> object(stdClass)#17 (2) 
            { ["DateCreated"]=> string(19) "2016-07-06T09:36:03" ["CurrencyCode"]=> string(3) "USD" } 

            [1]=> object(stdClass)#18 (2) 
            { ["DateCreated"]=> string(19) "2016-12-07T02:49:02" ["CurrencyCode"]=> string(3) "USD" } 

            [2]=> object(stdClass)#19 (2) 
            { ["DateCreated"]=> string(19) "2016-12-07T02:52:38" ["CurrencyCode"]=> string(3) "USD" } 

            [3]=> object(stdClass)#20 (2) 
            { ["DateCreated"]=> string(19) "2016-12-07T02:53:38" ["CurrencyCode"]=> string(3) "USD" }

        }
    }
}

我需要的是一个foreach循环,我可以检索每个键和值:

DateCreated: 2016-07-06T09:36:03
CurrencyCode: USD

我尝试使用json_encode($response)删除了object(stdClass)#15 (1)json_dencode($response),这样就达到了这一点:

array(1) 
{ ["GetDatabaseResult"]=> array(1) 
    { ["DatabaseInformation"]=> array(4) 
        {   [0]=> array(2) 
            { ["DateCreated"]=> string(19) "2016-07-06T09:36:03" ["CurrencyCode"]=> string(3) "USD" }

            [1]=> array(17) 
            { ["DateCreated"]=> string(19) "2016-12-07T02:49:02" ["CurrencyCode"]=> string(3) "USD" } 

            [2]=> array(17) 
            { ["DateCreated"]=> string(19) "2016-12-07T02:52:38" ["CurrencyCode"]=> string(3) "USD" }

            [3]=> array(17) 
            { ["DateCreated"]=> string(19) "2016-12-07T02:52:38" ["CurrencyCode"]=> string(3) "USD" }

        }
    }
}

我知道它是一个嵌套数组,但是我该如何解析呢?

1 个答案:

答案 0 :(得分:1)

无需编码/解码循环。试试这个:

foreach($response->GetDatabaseResult->DatabaseInformation as $entry){
  error_log("Date Created: ".$entry->DateCreated."; Currency Code: ".$entry->CurrencyCode);
}