如何使用php的Soap库在Soap头中的节点上添加属性?
尝试了所有可用的示例(包括StackOverflow和php.net(示例,注释等)。
$a = ['_'=> 'bar', 'id' => 1, 'val' => 'foo'];
$x = new SoapVar($a,SOAP_ENC_OBJECT);
$h = new SoapHeader('http://www.w3.org/2003/05/soap-envelope','FFS', $x);
这导致:
<env:FFS>
<_>bar</_>
<id>1</id>
<val>foo</val>
</env:FFS>
附加:属性应该是命名空间。将名称空间节点添加到没有属性的正文和/或标题不是一个问题。向节点添加属性是一个问题。
结果节点我需要在结尾处看起来像
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:nsx="http://example.com">
<env:Header>
<node nsx:foo="bar">
<nsx:baz>bam</nsx:baz>
</node>
</env:Header>
<env:Body>
[ snip ]
</env:Body>
</env:Envelope>
注意:由于我到目前为止发现的所有示例/提示都建议手动构建节点或使用RAW XML,我必须强调以下内容:
答案 0 :(得分:0)
找到了一种在WSDL模式下执行此操作的方法:
class FromSystemId {
protected $_ = "Bla bla bla";
protected $ID = "999";
}
class Auth {
/**
* @var FromSystemId
*/
protected $FromSystemId;
/**
* @var string
*/
protected $UserName;
/**
* @var string
*/
protected $Password;
/**
* Auth constructor.
* @param FromSystemId $FromSystemId
* @param string $UserName
* @param string $Password
*/
public function __construct(FromSystemId $FromSystemId, $UserName, $Password)
{
$this->FromSystemId = $FromSystemId;
$this->UserName = $UserName;
$this->Password = $Password;
}
}
try {
$soap = new SoapClient("some.wsdl", [
"location" => "some soap api url",
"soap_version" => SOAP_1_1,
"trace" => true,
"cache_wsdl" => WSDL_CACHE_NONE,
"classmap" => [
"Auth" => "Auth",
"FromSystemId" => "FromSystemId",
]
]);
$auth = new Auth(new FromSystemId(), "user", "password");
$soap->__setSoapHeaders(new SoapHeader("ns", "Auth", $auth));
结果将是:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="aa"
xmlns:ns2="bb">
<SOAP-ENV:Header>
<ns2:Auth xsi:type="ns1:Auth">
<ns1:FromSystemId ID="999">Bla bla bla</ns1:FromSystemId>
<ns1:UserName>user</ns1:UserName>
<ns1:Password>password</ns1:Password>
</ns2:Auth>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns1:GetCountriesRQ/>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>