2016-05-30 145 views
1

我是新來使用SOAP和PHP,我需要創建一個請求完全一樣使用PHP SoapClient的類:如何使用命名空間在SOAP客戶端XML請求

<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

我剛剛設法解決了這個問題 儘管我使用的代碼並未產生相同的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> 
相關問題