2017-08-25 141 views
0

我有以下的soap,它是我正在使用的API的响应。我正试图解析SOAP响应到JSON。在php中解析soap响应到json

到目前为止,我有这个肥皂,我已经投入变量$xml

$xml = '<?xml version="1.0" encoding="UTF-8"?> 
      <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> 
       <SOAP-ENV:Body> 
        <ns0:stkCallback xmlns:ns0="safaricom.co.ke/Schemas/STK/CallbackExternal"> 
         <ns0:MerchantRequestID>6h0rrD9ndK</ns0:MerchantRequestID> 
         <ns0:CheckoutRequestID>ws_CO_24082017105721840</ns0:CheckoutRequestID> 
         <ns0:ResultCode>0</ns0:ResultCode> 
         <ns0:ResultDesc>The service request has been accepted successfully.</ns0:ResultDesc> 
         <ns0:CallbackMetadata> 
          <ns0:Item> 
           <ns0:Name>Amount</ns0:Name> 
           <ns0:Value>1000.00</ns0:Value> 
          </ns0:Item> 
          <ns0:Item> 
           <ns0:Name>MpesaReceiptNumber</ns0:Name> 
           <ns0:Value>LHO31AMTRJ</ns0:Value> 
          </ns0:Item> 
          <ns0:Item> 
           <ns0:Name>Balance</ns0:Name> 
           <ns0:Value/> 
          </ns0:Item> 
          <ns0:Item> 
           <ns0:Name>TransactionDate</ns0:Name> 
           <ns0:Value>20170824105143</ns0:Value> 
          </ns0:Item> 
          <ns0:Item> 
           <ns0:Name>PhoneNumber</ns0:Name> 
           <ns0:Value>254713167623</ns0:Value> 
          </ns0:Item> 
         </ns0:CallbackMetadata> 
        </ns0:stkCallback> 
       </SOAP-ENV:Body> 
      </SOAP-ENV:Envelope>'; 

我需要创建一个可以<SOAP-ENV:Body>标签之间找到数据JSON响应。我怎样才能做到这一点?

+0

看一看[https://stackoverflow.com/questions/13245008/how-to-get-json-response-from-soap-call-in-php](https://stackoverflow。 com/questions/13245008/how-to-get-json-response-from-soap-call-in-php) –

回答

0

你可以试试吗?

// a bit of a hack, but let's see... 
list($trash,$xml)=explode('<soap:Body>',$xml); 
list($xml,$trash)=explode('</soap:Body>',$xml); 
unset($xml); 
$result=str_replace('xmlns="url"','',$xml); 

$simple_result=simplexml_load_string($xml); 
$json_result=json_encode($simple_result); 

//var_export($simple_result); 
echo $json_result; 
+0

这不起作用 –