简体   繁体   中英

PHP - Creating Nested Soap Headers using SoapClient

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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