如何将 SOAP 转换为 JSON?

缺口

我有一个正在尝试转换为 JSON 的soap 文件。我已经意识到我可以在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部分,并使用 将其转换为json simplexml_load_string

迪赞加

无法解析 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);
?>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章