如何在PHP中获取此soap标头和语法

时间:2015-08-05 11:43:03

标签: php soap namespaces

我需要实现以下

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ret="http://retailexpress.com.au/">
  <soapenv:Header>
  <ret:ClientHeader>
     <!--Optional:-->
     <ret:ClientID>9bf6dd42-35b9-46dd-948a-1c3c91906caa</ret:ClientID>
     <!--Optional:-->
     <ret:UserName>wsi</ret:UserName>
     <!--Optional:-->
     <ret:Password>wsipass</ret:Password>
  </ret:ClientHeader>

我得到了什么:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:ns1="http://retailexpress.com.au/" xmlns:ns2="namespace">
<SOAP-ENV:Header>
 <ns2:ClientHeader>
 <ns1:UserName>wsi</ns1:UserName>
 <ns1:Password>wsipass</ns1:Password>
 <ns1:ClientID>9bf6dd42-35b9-46dd-948a-1c3c91906caa</ns1:ClientID>   
 </ns2:ClientHeader>
</SOAP-ENV:Header>

如果我将“ns2:ClientHeader”的两个实例更改为ns1:ClientHeader,那么代码工作正常,我得到了必要的输出。但我不知道如何避免在那里添加ns2而不是ns1。

这是我的PHP代码:

<?php
$client_id='9bf6dd42-35b9-46dd-948a-1c3c91906caa';
$wsdl_url="http://v2wsisandbox.retailexpress.com.au/dotnet/admin/webservices/v2/webstore/service.asmx?wsdl";
$user='wsi';
$pass='wsipass';    

$options_arr=array('trace' => TRUE);

$client = new SoapClient($wsdl_url, $options_arr);

$headerParams = array('ns1:UserName'    => $user,
                  'ns1:Password'      => $pass,
                  'ns1:ClientID'      => $client_id);

$soapStruct = new SoapVar($headerParams, SOAP_ENC_OBJECT);

$header = new SoapHeader('namespace', 'ClientHeader', $soapStruct, false);

$client->__setSoapHeaders($header);

我无法理解。过去几天一直在尝试,在我最终来到这里之前搜索了很多帖子。希望你们像往常一样帮助我。

谢谢。

2 个答案:

答案 0 :(得分:0)

请参阅此评论:

http://www.php.net/manual/en/soapclient.setsoapheaders.php#93460

所以它会像:

$headerbody = array('UsernameKey'=>array('Username'=>$UserID,
                                         'Password'=>$Pwd)); 
$header = new SOAPHeader($ns, 'RequestorCredentials', $headerbody);       

//set the Headers of Soap Client.
$soap_client->__setSoapHeaders($header); 

答案 1 :(得分:0)

SoapHeader中的命名空间需要匹配ns1的命名空间。在你的情况下它是:

xmlns:ns1="http://retailexpress.com.au/"

所以你的SoapHeader命名空间应该是:

http://retailexpress.com.au/

示例:

$header = new SoapHeader('http://retailexpress.com.au/', 'ClientHeader', $soapStruct, false);