Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP SoapClient and a complex header

Tags:

php

soap

I have been trying for a while now, but I can't figure out how to use PHP SoapCLient in combination with a WSDL to form a complex header (that is, more complex than any tutorial I could find). The envelope that I need to send looks like this:

(001) <?xml version='1.0' encoding='UTF-8'?>

(002) <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xlink="http://www.w3.org/1999/xlink">

(003) <SOAP-ENV:Header>

(004) <eb:MessageHeader xmlns:eb="http://www.ebxml.org/namespaces/messageHeader" eb:version="2.0" SOAP-ENV:mustUnderstand="1">

(005) <eb:From>

(006) <eb:PartyId>webservices.example.com</eb:PartyId> 

(007) </eb:From>

(008) <eb:To>

(009) <eb:PartyId>clientURL</eb:PartyId> 

(010) </eb:To>

(011) <eb:CPAId>IPCC</eb:CPAId> 

(012) <eb:ConversationId>[email protected]</eb:ConversationId> 

(013) <eb:Service eb:type="XML">Session</eb:Service> 

(014) <eb:Action>SessionCreateRS</eb:Action> 

(015) <eb:MessageData>

(016) <eb:MessageId>mid:[email protected]</eb:MessageId> 

(017) <eb:Timestamp>2001-02-15T11:25:12Z</eb:Timestamp> 

(018) </eb:MessageData>

(019) <RefToMessageId>mid:20001209-133003-2333@clientURL</RefToMessageId> 

(020) </eb:MessageHeader>

(021) <wsse:Security xmlns:wsse="http://schemas.xmlsoap.org/ws/2002/12/secext">

(022) <wsse:BinarySecurityToken xmlns:wsu="http://schemas.xmlsoap.org/ws/2002/12/utility" wsu:Id="SabreSecurityToken" valueType="String" EncodingType="wsse:Base64Binary">Shared/IDL:IceSess\/SessMgr:1\.0.IDL/Common/!ICESMS\/RESC!ICESMSLB\/RES.LB!-4954987477210575357!252506!0</wsse:BinarySecurityToken> 

(023) </wsse:Security>

(024) </SOAP-ENV:Header>

(025) <SOAP-ENV:Body>

(026) <eb:Manifest xmlns:eb="http://www.ebxml.org/namespaces/messageHeader" eb:version="2.0">

(027) <eb:Reference eb:id="SessionCreateRS" xlink:type="simple" xlink:href="cid:SessionCreateRS"

(028) <eb:Description xml:lang="en-US">Response Message</eb:Description>"/>

(029) </eb:Reference>

(030) </eb:Manifest>

(031) </SOAP-ENV:Body>

(032) </SOAP-ENV:Envelope>

I tried to make it using __setSoapHeaders() and using SoapVar(). I can't get the right output though. From examples I learned it would be good to use a class to fill the parameters in the XML, so I made an class like this:

class EbXmlMessage{
 public $From = array('PartyId' => '[email protected]');
 public $To = array('PartyId' => 'example.com');
 public $CPAId = 'XXXX';
 public $ConversationId = '[email protected]';
 public $Service = 'Session';
 public $Action = 'SessionCreateRQ';
 public $MessageData = array( 'MessageId' => "mid:[email protected]",'Timestamp' => '2010-11-26T08:19:00Z');
}

than I use:

$eb_params = new SoapVar($eb,SOAP_ENC_OBJECT);
$header =  new SoapHeader($ns,"header", $eb_params,true);

But the request does not even slightly begin to look like it has to:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.opentravel.org/OTA/2002/11" xmlns:ns2="https://cert.webservices.sabre.com/cert">
 <SOAP-ENV:Header>
  <ns2:header SOAP-ENV:mustUnderstand="1">
   <From>
    <item>
     <key>PartyId</key>
     <value>[email protected]</value>
    </item>
   </From>
   <To>
    <item>
     <key>PartyId</key>
     <value>webservices.sabre.com</value>
    </item>
   </To>
   <CPAId>XXX</CPAId>
   <ConversationId>[email protected]</ConversationId>
   <Service>Session</Service>
   <Action>SessionCreateRQ</Action>
   <MessageData>
    <item>
     <key>MessageId</key>
     <value>mid:[email protected]</value>
    </item>
    <item>
     <key>Timestamp</key>
     <value>2010-11-26T08:19:00Z</value>
    </item>
   </MessageData>
  </ns2:header>
 </SOAP-ENV:Header>
 <SOAP-ENV:Body>
  <ns1:SessionCreateRQ>
   <ns1:POS>
    <ns1:Source PseudoCityCode="XXXX"/>
   </ns1:POS>
  </ns1:SessionCreateRQ>
  <param1/>
 </SOAP-ENV:Body>
</SOAP-ENV:Envelope

The main problems so far are getting the right tags in the XML on the right place, and I don't know how I can get "eb:" and "wsse:" namespace in the tags. I am hoping to use the SoapClientI() class but I'm not sure if it can handle a more complex XML like the one I need, if not maybe I should use Curl or something similar and just treat the XML as a string?

like image 715
Rijmen Avatar asked Nov 30 '10 21:11

Rijmen


1 Answers

I had issues with this as well when implementing wsse. Here was my approach. Yes, this is quite wsse-specific; however, it should work in your context as well. Note especially how there's the 'wsse' namespace parameter in the call to new SoapHeader(). Just replace that with 'eb' for your case.

$header_part = '
    <wsse:Security xmlns:wsse="http://schemas.xmlsoap.org/ws/2003/06/secext" SOAP-ENV:mustUnderstand="1">
        <wsse:UsernameToken>
            <wsse:Username>'."USERNAME".'</wsse:Username>
            <wsse:Password>'."PASSWORD".'</wsse:Password>
        </wsse:UsernameToken>
    </wsse:Security>
';
$soap_var_header = new SoapVar( $header_part, XSD_ANYXML, null, null, null );
$soap_header = new SoapHeader( 'http://your-target-service.com', 'wsse', $soap_var_header );
$soap_client->__setSoapHeaders($soap_header);

Another solution is to mangle the XML after subclassing the SoapClient, but I'd only do this as a last resort.

class My_SoapClient extends SoapClient {
   protected $_response = null;
   public function __doRequest( $request, $location, $action, $version, $one_way=null ) {
      // insert big bad mangling code here
      $this->_response = parent::__doRequest( $this->_request, $location, $action, $version, $one_way );
      return $this->_response;
   }
}
like image 100
zanlok Avatar answered Sep 19 '22 21:09

zanlok