0

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.

2 Answers 2

1

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]
    => ) )
Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

Using Re0sless approach seems to be working for me. I wanted to avoid writing out the XMLs. Thanks for another way to solve it though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.