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 关于#网络安全#的问题:求ensp的网络安全,不要步骤要完成版文件
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥20 使用Photon PUN2解决游戏得分同步的问题
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM