如何从Soap Request中读取值

时间:2017-09-18 09:10:48

标签: php xml soap

我正在与Soap Web服务进行交互,我想从soap对象中读取值。我一直都是个例外。见下文;

样品肥皂;

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Body>
      <c2b:C2BPaymentValidationRequest xmlns:c2b="http://cps.huawei.com/cpsinterface/c2bpayment">
         <TransID>3IN40005PY</TransID>
         <TransTime>20160923143639</TransTime>
         <TransAmount>3000</TransAmount>
         <BusinessShortCode>10069</BusinessShortCode>
         <MSISDN>86812530102</MSISDN>
         <KYCInfo>
            <KYCName>FirstName</KYCName>
            <KYCValue>aaa</KYCValue>
         </KYCInfo>
         <KYCInfo>
            <KYCName>MiddleName</KYCName>
            <KYCValue>bbb</KYCValue>
         </KYCInfo>
         <KYCInfo>
            <KYCName>LastName</KYCName>
            <KYCValue>ccc</KYCValue>
         </KYCInfo>
      </c2b:C2BPaymentValidationRequest>
   </soapenv:Body>
</soapenv:Envelope>

我尝试制作一个对象

$xml=simplexml_load_string($response) or die("Error: Cannot create object");

这样我就可以阅读下面的值;

$TransID=$xml->TransID; $TransTime=$xml->TransTime; $TransAmount=$xml->TransAmount;

但它在这里失败死亡(&#34;错误:无法创建对象&#34;);

甚至更好的我如何将soap请求更改为json并获取值?任何人吗?

1 个答案:

答案 0 :(得分:1)

您的代码中似乎缺少SOAP namespace declaration (php manual)


$xml = simplexml_load_string($xml_data);
$xml->registerXPathNamespace('c2b', 'http://cps.huawei.com/cpsinterface/c2bpayment');
$response = $xml->xpath('//c2b:C2BPaymentValidationRequest')  or die("Error: Cannot create object");

print_r($response);

<强>输出:


Array
(
    [0] => SimpleXMLElement Object
        (
            [TransID] => 3IN40005PY
            [TransTime] => 20160923143639
            [TransAmount] => 3000
            [BusinessShortCode] => 10069
            [MSISDN] => 86812530102
            [KYCInfo] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                            [KYCName] => FirstName
                            [KYCValue] => aaa
                        )

                    [1] => SimpleXMLElement Object
                        (
                            [KYCName] => MiddleName
                            [KYCValue] => bbb
                        )

                    [2] => SimpleXMLElement Object
                        (
                            [KYCName] => LastName
                            [KYCValue] => ccc
                        )

                )

        )

)