dongshi2141 2014-08-20 10:38
浏览 55
已采纳

将SOAP请求从XML转换为PHP

I am getting desperate here, I am trying to convert an XML SOAP request which works in SoapUI to PHP. I went through so much documentation online and still cannot get to creating a correct request from within PHP. I tried using the SOAP class provided in PHP together SoapVars, SoapParams and SoapHeaders. This is the request I need to send (in XML form):

<soapenv:Envelope 
xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' 
xmlns:soapenc='http://schemas.xmlsoap.org/soap/encoding/' 
xmlns:xsd='http://www.w3.org/2001/XMLSchema' 
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' 
xmlns:wsa='http://schemas.xmlsoap.org/ws/2004/03/addressing' 
xmlns:gen='http://www.polaris-uk.co.uk/GenericSchema/1_1/GenericTypes' 
xmlns:Detail='http://www.polaris-uk.co.uk/GenericSchema/2/PEMFault' 
xsi:SchemaLocation='http://www.polaris-uk.co.uk/GenericSchema/2/PEMFault http://ppw.imarket.co.uk/Polaris/Schema/PEMFault.xsd'>
<soapenv:Header>
  <wsse:Security 
  xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis- 200401-wss-wssecurity-secext-1.0.xsd" 
  soapenv:mustUnderstand="1" 
  soapenv:actor="http://www.imarket.co.uk/soap/actor">
    <wsse:UsernameToken>
      <wsse:Username>XXXXXXXXXX</wsse:Username>
      <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-ns2curity-secext-1.0.xsd#PasswordText">XXXXXXXXXX</wsse:Password>
    </wsse:UsernameToken>
  </wsse:Security>
</soapenv:Header>

<soapenv:Body>
  <ConfirmImarketUserIDReq xmlns="http://www.polaris-uk.co.uk/Schema/1_1/ConfirmImarketUserIDReq"> 
    <ns1:UserID xmlns:ns1="http://www.polaris-uk.co.uk/GenericSchema/1_1/GenericTypes">XXXXXXXXXX</ns1:UserID> 
    <ns2:Password xmlns:ns2="http://www.polaris-uk.co.uk/GenericSchema/1_1/GenericTypes">XXXXXXXXXX</ns2:Password> 
  </ConfirmImarketUserIDReq>
</soapenv:Body>
</soapenv:Envelope>

I will not post any code I did until now because it is just a mess since I tried writing bits and pieces without actually putting them together at one point and it will just spam this whole post.

Please, if anyone could help with converting this into a PHP code it will be much appreciated.

Thank you in advance!

  • 写回答

1条回答 默认 最新

  • duanping6698 2014-08-21 15:31
    关注

    After headaches over this, I finally found a working solution, might not be the best but it works by providing Raw XML to the SOAP call:

    $wsdl   = "XXXX.wsdl"; 
    $client = new SoapClient($wsdl, array(  'soap_version' => SOAP_1_1,'trace' => true,)); 
    
    //=========== Header Settings ============\\
    
    //Namespace of the Web Service
    $namespace = 'http://schemas.xmlsoap.org/soap/envelope/'; 
    
    $headerXML = <<<XML
    <wsse:Security 
      xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" 
      SOAP-ENV:mustUnderstand="1" SOAP-ENV:actor="http://www.imarket.co.uk/soap/actor">
        <wsse:UsernameToken>
          <wsse:Username>XXXXXX</wsse:Username>
          <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-ns2curity-secext-1.0.xsd#PasswordText">XXXXXX</wsse:Password>
        </wsse:UsernameToken>
    </wsse:Security>
    XML;
    
    $headerObj = new SoapVar($headerXML, XSD_ANYXML, null, null, null);
    $header = new SoapHeader($namespace , 'SessionHeader', $headerObj);
    
    // More than one header can be provided in this array.
    $client->__setSoapHeaders(array($header));
    
    //============== Request ================\\
    
    $xml = <<<XML
      <ConfirmImarketUserIDReq xmlns="http://www.polaris-uk.co.uk/Schema/1_1/ConfirmImarketUserIDReq"> 
        <ns1:UserID xmlns:ns1="http://www.polaris-uk.co.uk/GenericSchema/1_1/GenericTypes">XXXXXX</ns1:UserID> 
        <ns2:Password xmlns:ns2="http://www.polaris-uk.co.uk/GenericSchema/1_1/GenericTypes">XXXXXX</ns2:Password> 
      </ConfirmImarketUserIDReq>
    XML;
    
    $args = array(new SoapVar($xml, XSD_ANYXML));
    
    try {
    
        $response = $client->__soapCall( 'ConfirmImarketUserIDOp', $args);
        var_dump($response);
    }
    catch (SoapFault $e) {
      trigger_error("SOAP Fault: (faultcode: {$e->faultcode}, faultstring: {$e->faultstring})", E_USER_ERROR);
    }
    

    Hope this helps anyone, Cheers!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 wamp3.3.5安装完成后图标正常显示绿色,鼠标左右键点击图标均无反应。求解决方法。
  • ¥15 鼠标点击的这条记录了什么?
  • ¥15 在写pid调速的程序时,电机始终维持最大速度
  • ¥15 【有偿】调用DXGI截图初始化失败,提示0xf 887a0004
  • ¥15 请问如何查看手机root记录?
  • ¥15 商城小程序订单号重复
  • ¥15 学校优化算法sbo和蚁群算法怎么结合
  • ¥21 matlab怎么求时域信号的二阶导数
  • ¥15 判断两个表是否完全相同
  • ¥15 java map类型数据格式,如何快速通过前缀匹配元素