如何将SOAP转换为JSON?

时间:2017-07-08 13:27:56

标签: php json xml soap

我有一个肥皂文件,我试图转换为JSON。我已经意识到我可以使用simplexml_load_string没有命名空间,如下所示

$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>

如何从上面的soap响应中取出信封部分,以便我只能保留我可以轻松存储在变量中的xml部分,并使用simplexml_load_string将其转换为json?

1 个答案:

答案 0 :(得分:0)

  

无法解析json,因为您的xml文件包含“ xml   命名空间“。使用Regex,你可以通过选择来解析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);
?>
相关问题