(PHP 5 >= 5.0.5, PHP 7)

SoapClient::__setSoapHeaders以降のコール用の SOAP ヘッダを設定する


public SoapClient::__setSoapHeaders ([ mixed $soapheaders ] ) : bool

SOAP リクエストで送信するヘッダを定義します。





設定したいヘッダ。SoapHeader オブジェクト、あるいは SoapHeader オブジェクトの配列です。 省略したり NULL を設定したりした場合はヘッダが削除されます。


成功した場合に TRUE を、失敗した場合に FALSE を返します。

例1 SoapClient::__setSoapHeaders() の例


= new SoapClient(null, array('location' => "http://localhost/soap.php",
'uri'      => "http://test-uri/"));
$header = new SoapHeader(''
'hello world');



例2 複数のヘッダの設定


= new SoapClient(null, array('location' => "http://localhost/soap.php",
'uri'      => "http://test-uri/"));
$headers = array();

$headers[] = new SoapHeader(''
'hello world');

$headers[] = new SoapHeader(''
'hello world again');



add a note add a note

User Contributed Notes 4 notes

kedar dot purohit @ mavs dot uta dot edu
9 years ago
To create complex SOAP Headers, you can do something like this:

Required SOAP Header:

    <RequestorCredentials xmlns="">

Corresponding PHP code:


= ''; //Namespace of the WS.

//Body of the Soap Header.
$headerbody = array('Token' => $someToken,
'Version' => $someVersion,

//Create Soap Header.       
$header = new SOAPHeader($ns, 'RequestorCredentials', $headerbody);       
//set the Headers of Soap Client.

peamik1953 at NOSPAM dot btinternet dot com
8 years ago
You cannot add an additional header. If you want two headers, and one already exists, first delete it with $client->__setSoapHeaders(NULL). Then issue $client->__setSoapHeaders($headers) where $headers is an array of soapHeader() objects.
jayrajput at gmail dot com
10 years ago
With multiple SOAP headers, when using SoapVar for creation of SoapHeader the PHP code just terminates (command terminated). I am not sure if that is a bug.

Without the SOAPVar the code worked fine for me

There are different way to creart SoapHeader I was using SoapVar and the code was not working. I am still a novice with this SOAP stuff.

Tried using normal strings and it worked fine. SoapHeader can take SoapVar or string as the third argument.

my code:

// first soap header.
$var = new SoapVar($header, XSD_ANYXML);
$soapHeader = new SoapHeader(NAME_SPACE, "Security", $var);
// second soap header.
$var2 = new SoapVar($header2, XSD_ANYXML);
$soapHeader2 = new SoapHeader(DIFF_NAME_SPACE, "ID", $var2);

$client = new SoapClient($wsdl, array("location" => $location));

$headers = array();
$headers[] = $soapHeader;
$headers[] = $soapHeader2;

// Here my code was just terminating.
mlconnor at yahoo dot com
8 years ago
Does anyone know how to get the response headers?  The getLastResponseHeader returns a string response, not the complex object I was expecting...
To Top