PHP - 使用 SoapClient 创建嵌套 Soap 标头

发布于 2024-11-26 19:50:55 字数 779 浏览 2 评论 0原文

我正在 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"
    )
);

但这会引发致命错误。

I am using the SoapClient class in PHP to make a request. I am trying to create a nested header variable to pass in my request.

Currently my request headers looks like this,

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

I want it to look like this,

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

I am currently creating the headers like this,

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

I have tried various ways to nest the headers the including,

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

but this throws a fatal error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

逐鹿 2024-12-03 19:50:55

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

Object:

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

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

print_r ($headers1);
?>

Gives

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

Array:

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

    print_r ($headers2);
?>

Gives

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

You can try using an object or an array as the $data parameter

Object:

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

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

print_r ($headers1);
?>

Gives

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

Array:

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

    print_r ($headers2);
?>

Gives

Array ( [0] => SoapHeader Object ( [namespace] => n1 [name] => ei
    [data] => Array ( [type] => 123 [value] => UNKNOWN ) [mustUnderstand]
    => ) )
倾`听者〃 2024-12-03 19:50:55

您无法创建嵌套实例,您可以尝试类似的方法

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

You can't create nested instance, you can try something like these

//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); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文