8

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

4

2 回答 2

7

我需要类似的东西,并且能够使用 XSD_ANYXML SoapVar 来实现这一点:

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

这导致:

<SOAP-ENV:Header>
   <username>12345</username>
   <password>12</password>
</SOAP-ENV:Header>
于 2015-07-14T19:16:05.120 回答
2

在您的示例中,您只创建了一个 SoapHeader 条目(具有命名空间,但名为“null”)。您想要的结果包含两个单独的标题条目(没有命名空间),因此您可以尝试:

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

然后,您将$headers数组传递给soap 调用(直接或预先通过__setSoapHeaders)。

于 2010-03-27T12:26:50.230 回答