Custom Header Using PHP Soap Functions

I had a problem getting a custom soap header to work with PHP5. Can anybody help me.

I need something like this

<SOAP-ENV:Header> <USER>myusername</USER> <PASSWORD>mypassword</PASSWORD> </SOAP-ENV:Header> 

I get:

 <SOAP-ENV:Header> <ns2:null> <USER>myusername</USER> <PASSWORD>mypassword</PASSWORD> </ns2:null> </SOAP-ENV:Header> 

I would like to remove namespace tags. The code I use for this:

 class Authstuff { public $USER; public $PASSWORD; public function __construct($user, $pass) { $this->USER = $user; $this->PASSWORD = $pass; } } $auth = new Authstuff('myusername', 'mypassword'); $param = array('Authstuff' => $auth); $authvalues = new SoapVar($auth,SOAP_ENC_OBJECT); $header = new SoapHeader('http://soapinterop.org/echoheader/',"null",$authvalues); 

Zero does not seem to pass .. with 'null' I still get a namespace, as in the second example .. how to exclude this NS ... thanks for your help in advance.

 $headers = array(); $headers[] = new SoapHeader(null, 'USER', $username); $headers[] = new SoapHeader(null, 'PASSWORD', $password); $client->__setSoapHeaders($headers); try { $result = $client->getAvailableLicensedDNCount('ASX01'); print_r($result); 

Fatal error: SoapHeader :: SoapHeader (): Invalid parameters. Invalid namespace. at / usr / home / deepesh / SoapCalls / deepesh 7.php on line 29

+6
soap php soap-client
source share
2 answers

I needed something similar, and I was able to use XSD_ANYXML SoapVar for this:

  $auth = "<username>$username</username>"; $auth .= "<password>$password</password>"; $auth_block = new SoapVar( $auth, XSD_ANYXML, NULL, NULL, NULL, NULL ); $header = new SoapHeader( 'http://schemas.xmlsoap.org/soap/envelope/', 'Header', $auth_block ); $soap_client->__setSoapHeaders( $header ); 

The result is:

 <SOAP-ENV:Header> <username>12345</username> <password>12</password> </SOAP-ENV:Header> 
+3
source share

In your example, you only create one SoapHeader record (with a namespace, but with a name of "null"). Your desired result contains two separate header entries (without a namespace), so you can try:

 $headers = array(); $headers[] = new SoapHeader(NULL, 'USER', $auth->USER); $headers[] = new SoapHeader(NULL, 'PASSWORD', $auth->PASSWORD); 

Then you pass the $headers array to the soap call (directly or at the top through __setSoapHeaders ).

+2
source share

All Articles