简体   繁体   English

PHP SoapClient-如何构建Soap Header

[英]PHP SoapClient - How to Structure Soap Header

Using SoapClient in PHP 5.3.28 would like to create a soap header that looks like: 在PHP 5.3.28中使用SoapClient想要创建一个如下的soap标头:

<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. 我到处搜索,关于PHP soapclient如何从数据结构创建标头似乎没有太多信息。 Any idea how I can get the header format I want using SoapClient? 知道如何使用SoapClient获得所需的标题格式吗? Any pointers appreciated. 任何指针表示赞赏。

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

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. 感谢Feroz提出的答案,如果您在__soapCall中发送参数,whitch btw可以工作,但是在创建标头时却不起作用。

Thanks also to cb for the solution: http://www.php.net/manual/en/soapvar.soapvar.php#91961 也感谢cb提供的解决方案: http : //www.php.net/manual/zh/soapvar.soapvar.php#91961

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM