sissonb
sissonb

Reputation: 3780

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.

Upvotes: 0

Views: 1926

Answers (2)

toopay
toopay

Reputation: 1635

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

Upvotes: 1

Re0sless
Re0sless

Reputation: 10886

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

Upvotes: 1

Related Questions