PHP SOAP Header构造

时间:2012-06-07 00:13:28

标签: php soap wsdl

所以基本上我希望SOAP标题是这样的:

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

但相反,现在我有了这个:

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

这是我目前使用的php代码:

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

我哪里出错了?我似乎无法正确构建标题。

5 个答案:

答案 0 :(得分:5)

尝试使用匿名类而不是数组:

// Use standard class and set magic properties.
$header = new stdClass();
$header->siteId = '0';
$header->Username = 'MEMBERSHIP';
$header->Password = 'P@ssw0rd';

答案 1 :(得分:2)

将参数数组转换为对象效果非常好

$auth = (object)array(
  'Username' => 'AzureDiamond',
  'Password' => 'hunter2',
);
$header = new SOAPHeader('http://my.ns/','loginDetails',$auth);

但是,我发现它仍然没有得到名称空间。以上产生

<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>
....

你会看到虽然'loginDetails'标签在请求的命名空间中,但是它的子元素不是。

答案 2 :(得分:1)

我刚遇到一个非常类似的问题。

要修复它,您需要使用SoapVar类。这允许您控制每个标记使用的命名空间。

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

您的请求的soap标题看起来应该是这样的:

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

答案 3 :(得分:0)

有错字:

$header = NEW stdClass() ;

答案 4 :(得分:-2)

$ headerbody = array(&#39; Id&#39; =&gt;&#39; 0&#39;,&#39;用户名&#39; =&gt;&#39;会员&#39;, &#39;密码&#39; =&GT; &#39,P @ ssw0rd&#39);

请参阅链接 http://php.net/manual/en/class.soapheader.php

还要确保在SOAPHeader构造函数

中传递正确的命名空间(第一个参数)