在PHP 5.3.28中使用SoapClient想要创建一个类似于:
的soap标头<soap:Header>
<ns:RequestParams Size="Large" Color="Blue" Brand="xyz">
</soap:Header>
如果我像这样构建标题:
$params = array('RequestParams' => array('Size' => 'Large', 'Color' => 'Blue', 'Brand' => 'xyz');
$header = new SoapHeader(NameSpace, 'RequestParams', $params);
$client = new SoapClient(NULL, array("location" => "https://endpoint-url",
"uri" => "http://namespace-uri",
"soap_version" => SOAP_1_2, "trace" => 1));
$client->__setSoapHeaders($header);
$result = $client->__soapCall(some soap call here);
echo $client->__getLastRequest() . "\n";
我得到的标题是:
<env:Header>
<ns2:RequestParams>
<item><key>RequestParams</key><value>
<item><key>Size</key><value>Large</value></item>
<item><key>Color</key><value>Blue</value></item>
<item><key>LastName</key><value>xyz</value></item></value>
</item>
</ns2:RequestParams>
</env:Header>
我收到服务器的回复,告诉我这是一个无效的标题。我一直在搜索,似乎没有太多关于PHP soapclient如何从数据结构创建标头的信息。知道如何使用SoapClient获取我想要的标题格式吗? 任何指针都赞赏。
答案 0 :(得分:0)
使用你可以使用数组吗
$parm = array(
'properties' => array(
'Size' => 'Large',
'Color' => 'Blue',
'Brand' => 'xyz'
), );
将创建此
<properties Size="Large" Color="Blue" Brand="xyz">
答案 1 :(得分:0)
无法找到任何直接的方法来创建带有params作为一个节点属性的标头。最后这个有用,但不是很漂亮:
$client = new SoapClient(NULL,
array('location' => $loc, 'uri' => $ns,
'soap_version' => SOAP_1_2,
'style' => SOAP_DOCUMENT));
$headerVar = new SoapVar('<ns1:RequestParams Size="Large" Color="Blue" Brand="xyz"/>',
XSD_ANYXML);
$header = new SoapHeader($ns, 'RequestParams', $headerVar);
$client->__setSoapHeaders($header);
$result = $client->__soapCall('SomeFunc', array(...));
感谢Feroz建议回答,如果你在__soapCall中发送参数,那么btw是否正常工作,那么在创建标题时就没有用。
还要感谢cb解决方案:http://www.php.net/manual/en/soapvar.soapvar.php#91961
答案 2 :(得分:-1)
怎么样
$headers =
[
"Content-Type: text/xml; charset=utf-8",
"Accept: text/xml",
"Cache-Control: no-cache",
"Pragma: no-cache",
"SOAPAction:" . '"' . $soapAction . '"',
"Content-length: " . strlen($xml)
];