猿问

如何在 PHP 中将 XML 转换为数组

我是 SOAP API 的新手,我得到了 Web 服务的响应。但我无法将其转换为数组以获取密钥。


<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">

   <soap:Body>

      <ns2:setTransactionResponse xmlns:ns2="http://soap.api.controller.web.payjar.com/">

         <return>

            <merchantReference>mercRef_1395758213</merchantReference>

            <payUReference>17613281409117</payUReference>

            <successful>true</successful>

         </return>

      </ns2:setTransactionResponse>

   </soap:Body>

</soap:Envelope>


DIEA
浏览 133回答 1
1回答

慕桂英546537

尝试以下对数组的 xml 响应&nbsp;$xml = $response;$xml = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $xml);$xml = simplexml_load_string($xml);$json = json_encode($xml);$responseArray = json_decode($json,true);echo "<pre>";print_r($responseArray);输出Array(&nbsp; &nbsp; [soapBody] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [ns2setTransactionResponse] => Array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [return] => Array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [merchantReference] => mercRef_1395758213&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [payUReference] => 17613281409117&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [successful] => true&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; ))
随时随地看视频慕课网APP
我要回答