Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom header using PHP soap functions

I am having a problem getting a custom soap header to work with PHP5. Can anybody guide me please.

What I require is something like this

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

What I get is :

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

I would like to remove the namespace tags. The code I use to get this is:

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);

Null doesn't seem to pass.. with 'null' I still get name space as in 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. in /usr/home/deepesh/SoapCalls/deepesh7.php on line 29

like image 604
Dees Avatar asked Mar 26 '10 14:03

Dees


People also ask

How do you set headers in SOAP request?

Select the service task or web service integration component and click the Variables tab above the diagram area. Create the private variable that you will later map to the SOAP header of the request message. To add a single header entry to the request message, use the variable type SOAPHeader.

What is header in SOAP request?

The SOAP header is an optional section in the SOAP envelope, although some WSDL files require that a SOAP header is passed with each request. A SOAP header contains application-specific context information (for example, security or encryption information) that is associated with the SOAP request or response message.

Is header optional in SOAP?

The SOAP <Header> is an optional element in a SOAP message. It is used to pass application-related information that is to be processed by SOAP nodes along the message path. The immediate child elements of the <Header> element are called header blocks.

How can make SOAP call in PHP?

To make SOAP requests to the SOAP API endpoint, use the "Content-Type: application/soap+xml" request header, which tells the server that the request body contains a SOAP envelope. The server informs the client that it has returned a SOAP envelope with a "Content-Type: application/soap+xml" response header.


2 Answers

I needed something similar and was able to use an XSD_ANYXML SoapVar to achieve 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 );

This resulted in:

<SOAP-ENV:Header>
   <username>12345</username>
   <password>12</password>
</SOAP-ENV:Header>
like image 92
syndicate_software Avatar answered Oct 22 '22 16:10

syndicate_software


In your example, you are creating only one SoapHeader entry (with namespace, but named 'null'). Your desired result contains two separate header entries (without namespace), so you might try:

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

You'd then pass the $headers array to the soap call (either directly, or upfront via __setSoapHeaders).

like image 22
Henrik Opel Avatar answered Oct 22 '22 17:10

Henrik Opel