2017-07-08 44 views
1

我有一個肥皂文件,我試圖轉換爲JSON。我已經意識到,我可以使用simplexml_load_string沒有命名空間,如下如何將SOAP轉換爲JSON?

$xml = simplexml_load_string($soap_string); 
$json = json_encode($xml); 
header('Content-Type: application/json'); 
echo $json; 

我的回答下面的XML命名空間有,因此當我嘗試使用simplexml_load_string拋出錯誤。

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> 
     <soapenv:Body> 
      <res:ResultMsg xmlns:res="http://api-v1.gen.mm.vodafone.com/mminterface/result"> 
       <![CDATA[ 
       <?xml version="1.0" encoding="UTF-8"?> 
       <Result xmlns="http://api-v1.gen.mm.vodafone.com/mminterface/result"> 
        <ResultType>0</ResultType> 
        <ResultCode>0</ResultCode> 
        <ResultDesc>The service request has been accepted successfully.</ResultDesc> 
        <OriginatorConversationID>555010_jYJlV0MP3y</OriginatorConversationID> 
        <ConversationID>20170705_00007d59ab9111033601</ConversationID> 
        <TransactionID>LG51195MWH</TransactionID> 
        <ResultParameters> 
         <ResultParameter> 
         <Key>TransactionAmount</Key> 
         <Value>100</Value> 
         </ResultParameter> 
         <ResultParameter> 
         <Key>TransactionReceipt</Key> 
         <Value>LG51195MWH</Value> 
         </ResultParameter> 
         <ResultParameter> 
         <Key>B2CRecipientIsRegisteredCustomer</Key> 
         <Value>Y</Value> 
         </ResultParameter> 
         <ResultParameter> 
         <Key>B2CChargesPaidAccountAvailableFunds</Key> 
         <Value>-55.00</Value> 
         </ResultParameter> 
         <ResultParameter> 
         <Key>ReceiverPartyPublicName</Key> 
         <Value>254713171292 - test test</Value> 
         </ResultParameter> 
         <ResultParameter> 
         <Key>TransactionCompletedDateTime</Key> 
         <Value>05.07.2017 08:27:38</Value> 
         </ResultParameter> 
         <ResultParameter> 
         <Key>B2CUtilityAccountAvailableFunds</Key> 
         <Value>32526.00</Value> 
         </ResultParameter> 
         <ResultParameter> 
         <Key>B2CWorkingAccountAvailableFunds</Key> 
         <Value>541703.00</Value> 
         </ResultParameter> 
        </ResultParameters> 
        <ReferenceData> 
         <ReferenceItem> 
         <Key>QueueTimeoutURL</Key> 
         <Value>https://localhost:443/pay/timeout.php</Value> 
         </ReferenceItem> 
        </ReferenceData> 
       </Result> 
       ]]> 
      </res:ResultMsg> 
    </soapenv:Body> 
    </soapenv:Envelope> 

如何取出信封部分從上面,這樣我可以保持只有我可以在變輕鬆地存儲和隱蔽它使用simplexml_load_string到JSON的XML部分SOAP響應?

+0

使用SOAP API(EXT /皁)。 – ThW

回答

0

JSON的無法解析,因爲你的XML文件中包含 「XML命名空間 」。使用正則表達式,您可以通過選擇 「結果」標記(基本方式)來解析json。

如果你可以測試它,你可以編輯你需要的正則表達式模式。

<?php 
header('Content-type: application/json'); 
$soap_string = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> 
    <soapenv:Body> 
     <res:ResultMsg xmlns:res="http://api-v1.gen.mm.vodafone.com/mminterface/result"> 
      <![CDATA[ 
      <?xml version="1.0" encoding="UTF-8"?> 
      <Result xmlns="http://api-v1.gen.mm.vodafone.com/mminterface/result"> 
       <ResultType>0</ResultType> 
       <ResultCode>0</ResultCode> 
       <ResultDesc>The service request has been accepted successfully.</ResultDesc> 
       <OriginatorConversationID>555010_jYJlV0MP3y</OriginatorConversationID> 
       <ConversationID>20170705_00007d59ab9111033601</ConversationID> 
       <TransactionID>LG51195MWH</TransactionID> 
       <ResultParameters> 
        <ResultParameter> 
        <Key>TransactionAmount</Key> 
        <Value>100</Value> 
        </ResultParameter> 
        <ResultParameter> 
        <Key>TransactionReceipt</Key> 
        <Value>LG51195MWH</Value> 
        </ResultParameter> 
        <ResultParameter> 
        <Key>B2CRecipientIsRegisteredCustomer</Key> 
        <Value>Y</Value> 
        </ResultParameter> 
        <ResultParameter> 
        <Key>B2CChargesPaidAccountAvailableFunds</Key> 
        <Value>-55.00</Value> 
        </ResultParameter> 
        <ResultParameter> 
        <Key>ReceiverPartyPublicName</Key> 
        <Value>254713171292 - test test</Value> 
        </ResultParameter> 
        <ResultParameter> 
        <Key>TransactionCompletedDateTime</Key> 
        <Value>05.07.2017 08:27:38</Value> 
        </ResultParameter> 
        <ResultParameter> 
        <Key>B2CUtilityAccountAvailableFunds</Key> 
        <Value>32526.00</Value> 
        </ResultParameter> 
        <ResultParameter> 
        <Key>B2CWorkingAccountAvailableFunds</Key> 
        <Value>541703.00</Value> 
        </ResultParameter> 
       </ResultParameters> 
       <ReferenceData> 
        <ReferenceItem> 
        <Key>QueueTimeoutURL</Key> 
        <Value>https://localhost:443/pay/timeout.php</Value> 
        </ReferenceItem> 
       </ReferenceData> 
      </Result> 
      ]]> 
     </res:ResultMsg> 
</soapenv:Body> 
</soapenv:Envelope>'; 

//file_get_contents("http://localhost:24563/soap_string.xml"); // test xml url :) remove this comment 
$pattern = "(<Result(.+)>[\s\S]*?<\/Result>)"; 
preg_match_all($pattern, $soap_string, $matches); 

$xml = simplexml_load_string($matches[0][0]); 
echo json_encode($xml); 
?> 
+0

你自己試過了嗎?我的問題是不能將xml轉換爲json而是xml本身。想剝離 ' <![CDATA [' 和 ']]> ' – nick

+0

我已經更新了答案,你可以測試。 – dizaynplus

+1

謝謝你。完美的作品! – nick