2017-07-08 160 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