如何在SOAP客户端xml请求中使用名称空间

时间:2016-05-30 10:20:19

标签: php soap

我是使用SOAP和PHP的新手,我需要使用PHP SoapClient类创建一个完全相同的请求:

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" 
xmlns:ent="http://enterprise.000.000.com/Infrastructure/EnterpriseContext"
xmlns:ns="http://000.000.com/000/service/000/intf/1">
   <soap:Header>
      <ent:enterpriseContext>
         <ent:contextInfo>
            <ent:ProcessContextId>000K121300223393915419638486638404</ent:ProcessContextId>
            <ent:businessContextId>CO</ent:businessContextId>
            <ent:applicationContextId>3</ent:applicationContextId>
         </ent:contextInfo>
          <ent:requestOriginator>
            <ent:requesterCode>000</ent:requesterCode>
                <ent:machineIPAddress>0.0.0.0</ent:machineIPAddress>
                <ent:userPrincipleName>000</ent:userPrincipleName>
                <ent:requestedTimestamp>2015-10-01T05:53:04</ent:requestedTimestamp>
                <ent:channelId>1</ent:channelId>
         </ent:requestOriginator>
      </ent:enterpriseContext>
   </soap:Header>
   <soap:Body>
      <ns:InitiatePaymentDetails>
         <InitiatePaymentDetailsRequest>
            <transactionAmount>35</transactionAmount>
            <olpIdAlias>000_uat_uat1</olpIdAlias>
            <merchantRefNum>7999</merchantRefNum>
            <!--1 or more repetitions:-->
            <merchants>
               <merchantId>1003</merchantId>
               <merchantRefNum>7999</merchantRefNum>
               <paymentAmount>35</paymentAmount>
               <paymentCurrency>SAR</paymentCurrency>
               <merchantType>1</merchantType>
            </merchants>
            <merchantId>1003</merchantId>
         </InitiatePaymentDetailsRequest>
      </ns:InitiatePaymentDetails>
   </soap:Body>
</soap:Envelope>

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:1)

我设法解决了这个问题 虽然我使用的代码没有产生相同的xml请求,但它满足了Web服务要求。我把答案放在任何有类似问题的人身上。

$wsdl = 'https://000.000.com/000abpayproc-ws/000PaymentManager.wsdl';
        $client = new SoapClient($wsdl, array('trace' => 1));
        $unique_id = mt_rand(100000, 999999);
        $timestamp = time();
        $ref_number = mt_rand(10000000, 99999999);


        $headerbody = array(
            'contextInfo' => array(
                'ProcessContextId' => '000'.$unique_id.$timestamp,
                'businessContextId' => 'CO',
                'applicationContextId' => 3
            ),
            'requestOriginator' => array(
                'requesterCode' => '000',
                'machineIPAddress' => '00.00.00.00',
                'userPrincipleName' => '000',
                'requestedTimestamp' => '2015-10-01T05:53:04',
                'channelId' => 1
            ),
        );

        $ns = 'http://000.000.com/000/service/000/intf/1';
        $header = new SOAPHeader($ns, 'enterpriseContext', $headerbody);
        $ent = 'http://000.000.000.com/Infrastructure/EnterpriseContext';
        $header2 = new SOAPHeader($ent, 'enterpriseContext', $headerbody);


        $parm = array(
            'InitiatePaymentDetailsRequest' =>
            array(
                'transactionAmount' => 35,
                'olpIdAlias' => '000',
                'merchantRefNum' => $ref_number,
                'merchants' => array(
                    'merchantId' => 1003,
                    'merchantRefNum' => 7999,
                    'paymentAmount' => 35,
                    'paymentCurrency' => 'SAR',
                    'merchantType' => 1,
                ),
                'merchantId' => 1003
            )
        );

        try {
            $resp = $client->__soapCall('InitiatePaymentDetails', array($parm), array(), array($header, $header2));
            echo 'REQUEST:<br />';
            var_dump($client->__getLastRequest());
            echo '<br />';
            echo 'RESPONSE:<br />';
            dd($resp);
        } catch (SoapFault $ex) {
            echo 'REQUEST:<br />';
            $client->__getLastRequest();
            echo '<br />';
            echo 'RESPONSE:<br />';
            echo $ex->getMessage();
        }

结果请求如下:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://000.000.com/000/service/000/intf/1" xmlns:ns2="http://enterprise.000.000.com/Infrastructure/EnterpriseContext">
        <SOAP-ENV:Header>
            <ns1:enterpriseContext>
                <item>
                    <key>contextInfo</key>
                    <value>
                        <item>
                            <key>ProcessContextId</key>
                            <value>0004331921464611756</value>
                        </item>
                        <item>
                            <key>businessContextId</key>
                            <value>CO</value>
                        </item>
                        <item>
                            <key>applicationContextId</key>
                            <value>3</value>
                        </item>
                    </value>
                </item>
                <item>
                    <key>requestOriginator</key>
                    <value>
                        <item>
                            <key>requesterCode</key>
                            <value>000</value>
                        </item>
                        <item>
                            <key>machineIPAddress</key>
                            <value>00.00.00.00</value>
                        </item>
                        <item>
                            <key>userPrincipleName</key>
                            <value>000</value>
                        </item>
                        <item>
                            <key>requestedTimestamp</key>
                            <value>2015-10-01T05:53:04</value>
                        </item>
                        <item>
                            <key>channelId</key>
                            <value>1</value>
                        </item>
                    </value>
                </item>
            </ns1:enterpriseContext>
            <ns2:enterpriseContext>
                <ns2:contextInfo>
                    <ns2:ProcessContextId>004331921464611756</ns2:ProcessContextId>
                    <ns2:businessContextId>CO</ns2:businessContextId>
                    <ns2:applicationContextId>3</ns2:applicationContextId>
                </ns2:contextInfo>
                <ns2:requestOriginator>
                    <ns2:requesterCode>000</ns2:requesterCode>
                    <ns2:machineIPAddress>00.00.00.00</ns2:machineIPAddress>
                    <ns2:userPrincipleName>000</ns2:userPrincipleName>
                    <ns2:requestedTimestamp>2015-10-01T05:53:04</ns2:requestedTimestamp>
                    <ns2:channelId>1</ns2:channelId>
                </ns2:requestOriginator>
            </ns2:enterpriseContext>
        </SOAP-ENV:Header>
        <SOAP-ENV:Body>
            <ns1:InitiatePaymentDetails>
                <InitiatePaymentDetailsRequest>
                    <transactionAmount>35</transactionAmount>
                    <olpIdAlias>000</olpIdAlias>
                    <merchantRefNum>51777161</merchantRefNum>
                    <merchants>
                        <merchantId>1003</merchantId>
                        <merchantRefNum>7999</merchantRefNum>
                        <paymentAmount>35</paymentAmount>
                        <paymentCurrency>SAR</paymentCurrency>
                        <merchantType>1</merchantType>
                    </merchants>
                    <merchantId>1003</merchantId>
                </InitiatePaymentDetailsRequest>
            </ns1:InitiatePaymentDetails>
        </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>