问题
Using SoapClient in PHP 5.3.28 would like to create a soap header that looks like:
<soap:Header>
<ns:RequestParams Size="Large" Color="Blue" Brand="xyz">
</soap:Header>
If I construct the header like this:
$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";
The header I get is:
<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>
and I get a response from the server telling me it's an invalid header. I've searched around and there doesn't appear to be too much info on how PHP soapclient creates headers from data strctures. Any idea how I can get the header format I want using SoapClient? Any pointers appreciated.
回答1:
use can you use an array for this
$parm = array(
'properties' => array(
'Size' => 'Large',
'Color' => 'Blue',
'Brand' => 'xyz'
), );
will create this
<properties Size="Large" Color="Blue" Brand="xyz">
回答2:
Couldn't find any straightforward way to create a header with params as attributes of one node. In the end this works, though not very pretty:
$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(...));
Thanks to Feroz for suggesting an answer, whitch btw works if you are sending parameters in __soapCall, just didn't work when creating a header.
Thanks also to cb for the solution: http://www.php.net/manual/en/soapvar.soapvar.php#91961
回答3:
how about
$headers =
[
"Content-Type: text/xml; charset=utf-8",
"Accept: text/xml",
"Cache-Control: no-cache",
"Pragma: no-cache",
"SOAPAction:" . '"' . $soapAction . '"',
"Content-length: " . strlen($xml)
];
来源:https://stackoverflow.com/questions/22609718/php-soapclient-how-to-structure-soap-header