PHP - 使用SoapClient创建嵌套的Soap Headers

时间:2011-07-27 22:34:16

标签: php soap soap-client

我正在使用PHP中的SoapClient类来发出请求。我正在尝试创建一个嵌套的头变量来传递我的请求。

目前我的请求标题如下所示

<SOAP-ENV:Header>
    <ns2:type>request</ns2:type>
    ...
</SOAP-ENV:Header>

我希望它看起来像这样,

<SOAP-ENV:Header>
    <ns2:ei>
        <ns2:type>request</ns2:type>
        ...
    </ns2:ei>
</SOAP-ENV:Header>

我目前正在创建像这样的标题

$headers[] = new SoapHeader(
        $nsp1,
        "type",
        "request"
    );
$headers[] = new SoapHeader(...)

我尝试了各种方法来嵌套包含的标题,

$headers[] = new SoapHeader(
   $nsp1,
   "ei",
   new SoapHeader(
        $nsp1,
        "type",
        "request"
    )
);

但这会引发致命的错误。

2 个答案:

答案 0 :(得分:1)

您可以尝试使用对象或数组作为$ data参数

对象:

<?php
class MySoapHeader
{
    public $type = 123;
    public $value = 'UNKNOWN';
}

$headers1[] = new SoapHeader(
        'n1',
        'ei',
        new MySoapHeader()
    );

print_r ($headers1);
?>

给予

Array ( [0] => SoapHeader Object ( [namespace] => n1 [name] => ei 
    [data] => MySoapHeader Object ( [type] => 123 [value] => UNKNOWN ) 
    [mustUnderstand] => ) ) 

阵列:

<?php
    $headers2[] = new SoapHeader(
        'n1',
        'ei',
        array ('type'=>123,'value'=>'UNKNOWN')
    );

    print_r ($headers2);
?>

给出

Array ( [0] => SoapHeader Object ( [namespace] => n1 [name] => ei
    [data] => Array ( [type] => 123 [value] => UNKNOWN ) [mustUnderstand]
    => ) )

答案 1 :(得分:1)

您无法创建嵌套实例,您可以尝试这些

//Untested
$strHeaderComponent_Ei = "<ei><type>$strType</type></ei>";

$objVar_Ei_Inside = new SoapVar($strHeaderComponent_Ei, XSD_ANYXML, null, null, null);
$objHeader_Ei_Outside = new SoapHeader($nsp1, 'ei', $objVar_Ei_Inside);