使用php将xml响应转换为Array

时间:2014-05-19 10:20:44

标签: php xml web-services soap

请我开发一个php应用程序,我想知道如何将soap webservice的xml响应转换为字符串或数组或json,我想将webservice返回的值插入到我的数据库中。

这是我的xml respnse:

   <env:Envelope xmlns:env="">
   <env:Header/>
   <env:Body>
      <soap:getUserInfoResponse xmlns:soap="">
         <getUserInfoResult>
            <userInformation>
               <detailuserInformationList>
                  <detailuserInformation>
                     <Name>Alex</Name>
                     <alias>Lex</alias>
                     <age>24</age>
                     <function>agent</function>
                     <birthdate>1</birthdate>
                     <birthmounth>02</birthmounth>
                     <birthyear>1990</birthyear>
                     <address>wall street</address>
                     <civility>mr</civility>
                     <gender>m</gender>
                  </detailuserInformation>
               </detailuserInformationList>
               <lastLogin>14:22</lastLogin>
               <userId>A52241</userId>
               <cnxTimes>125</cnxTimes>
            </userInformation>
            <requestTag/>
         </getUserInfoResult>
      </soap:getUserInfoResponse>
   </env:Body>
</env:Envelope>

这是我的PHP代码,调用ws:

$client = new SoapClient('$url', array("trace" => 1, "exception" => 0));
$client->__setLocation('$url');

$response = $client->__soapCall("getUserInfo", array( parameters...);

print htmlspecialchars($client->__getLastResponse()); 

2 个答案:

答案 0 :(得分:0)

尝试使用php SoapClient类

SoapClient class

如果生成WSDL使用 NuSOAP

答案 1 :(得分:0)

首先,检查代码中$response的内容:

print htmlspecialchars($response); 

顺便说一句,这是正确的方式,或与SoapClient合作:

$client = new SoapClient($wsdl_url, array('trace' => 1, 'exception' => 0));
$response = $client->getUserInfo( /* parameters */ );

你会在$ response中获得数组。您可以使用$client->__getLastRequest()$client->__getLastResponse()来查看请求和响应XML。