2017-08-25 142 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

這不起作用 –