PHP SoapClient -如何构造Soap头



在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头格式?

use

$parm = array(
    'properties' => array(
        'Size' => 'Large',
        'Color' => 'Blue',
        'Brand' => 'xyz'
    ),  );

将创建这个

<properties Size="Large" Color="Blue" Brand="xyz">

找不到任何直接的方法来创建一个头与参数作为一个节点的属性。虽然不是很漂亮,但最终还是可以工作的:

$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中发送参数,只是在创建头时不起作用。

也感谢cb的解决方案:http://www.php.net/manual/en/soapvar.soapvar.php#91961

$headers = 
        [ 
           "Content-Type: text/xml; charset=utf-8",
           "Accept: text/xml",
           "Cache-Control: no-cache",
           "Pragma: no-cache",
           "SOAPAction:" . '"' . $soapAction . '"',
           "Content-length: " . strlen($xml)
       ];

相关内容

  • 没有找到相关文章

最新更新