Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP SOAP Header construction

Tags:

php

soap

wsdl

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.

like image 404
user1441041 Avatar asked Jun 07 '12 00:06

user1441041


3 Answers

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';
like image 67
Eric Jeker Avatar answered Nov 02 '22 00:11

Eric Jeker


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.

like image 21
dman Avatar answered Nov 01 '22 23:11

dman


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>
like image 1
Maxime Rainville Avatar answered Nov 01 '22 23:11

Maxime Rainville