user1441041
user1441041

Reputation: 51

PHP SOAP Header construction

So basically I want the SOAP header to be like this:

<soapenv:Header>
  <v1:loginDetails>
     <v11:Id>0</v11:Id>
     <v11:username>MEMBERS</v11:username>
     $ <v11:password>0x909711E5,0xE301F82A,0x0E2783CC,0xAF6BC3DB,0x57727CFB</v11:password>
  </v1:loginDetails>
   </soapenv:Header>
<soapenv:Body>
  <v11:GetNextAvailableMemberNumberRequest>
     <v11:Id>1</v11:Id>
     <v11:memberId>1</v11:memberId>
  </v11:GetNextAvailableNumberRequest>
   </soapenv:Body>
</soapenv:Envelope>

But instead, now I have this:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www..com/membership/types/v1_0">
<SOAP-ENV:Header>
<ns1:loginDetails>
<item><key>siteId</key><value>0</value></item>
<item><key>Username</key><value>MEMBERSHIP</value></item>
<item><key>Password</key><value>P@ssw0rd</value></item>
</ns1:loginDetails>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns1:GetNextAvailableMemberNumberRequest/>
<param1>1</param1>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

And this is the php code that I'm currently using:

$client = new SOAPClient('http://192.168.180.128:8010//membershipService?wsdl',array('trace' => true));
$client->__setSoapHeaders(null);
$headerbody = array ('siteId' => '0','Username' => 'MEMBERSHIP',
'Password' => 'P@ssw0rd');
$header = new SOAPHeader('http://www.com/club/services/membership/types/v1_0','loginDetails',$headerbody);
$client->__setSoapHeaders($header);

Where have I gone wrong? I seems unable to construct the header properly.

Upvotes: 5

Views: 5202

Answers (5)

Maxime Rainville
Maxime Rainville

Reputation: 2039

I just encountered a very similar problem.

To fix it, you need to use the SoapVar class. This allows you to control what namespace to use for each tag.

$client = new SOAPClient('http://192.168.180.128:8010//membershipService?wsdl',array('trace' => true));
$ns = 'http://www.com/club/services/membership/types/v1_0';
$headerbody = new SoapVar([
    new SoapVar('0', XSD_STRING, null, null, 'siteId', $ns),
    new SoapVar('MEMBERSHIP', XSD_STRING, null, null, 'Username', $ns),
    new SoapVar('P@ssw0rd', XSD_STRING, null, null, 'Password', $ns),
], SOAP_ENC_OBJECT);
$header = new SOAPHeader($ns,'loginDetails',$headerbody);
$client->__setSoapHeaders($header);

Your request's soap header should look something like this with this:

<SOAP-ENV:Header>
    <ns1:loginDetails>
        <ns1:siteId>0</ns2:siteId>
        <ns1:Username>MEMBERSHIP</ns2:Username>
        <ns1:Password>P@ssw0rd</ns2:Password>
    </ns1:loginDetails>
</SOAP-ENV:Header>

Upvotes: 1

Eric Jeker
Eric Jeker

Reputation: 546

Just try to use an anonymous class instead of an array :

// Use standard class and set magic properties.
$header = new stdClass();
$header->siteId = '0';
$header->Username = 'MEMBERSHIP';
$header->Password = 'P@ssw0rd';

Upvotes: 5

dman
dman

Reputation: 1099

Casting the array of parameters to an object works pretty well

$auth = (object)array(
  'Username' => 'AzureDiamond',
  'Password' => 'hunter2',
);
$header = new SOAPHeader('http://my.ns/','loginDetails',$auth);

However, I find that it still doesn't get namespaces quite right. The above produces

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"  xmlns:ns2="http://my.ns/">
  <SOAP-ENV:Header>
    <ns2:loginDetails>
      <Username>AzureDiamond</Username>
      <Password>hunter2</Password>
    </ns2:loginDetails>
  </SOAP-ENV:Header>
....

And you'll see that although the 'loginDetails' tag is in the requested namespace, it's child elements are not.

Upvotes: 2

Jack
Jack

Reputation: 1

There was typo:

$header = NEW stdClass() ;

Upvotes: 0

Umesh Chavan
Umesh Chavan

Reputation: 614

$headerbody = array ('Id' => '0','username' => 'MEMBERSHIP', 'password' => 'P@ssw0rd');

Please refer the link http://php.net/manual/en/class.soapheader.php

Also make sure that you are passing correct namespace (first parameter) in SOAPHeader constructor

Upvotes: -2

Related Questions