2024 年日本 PHP 研討會

SoapClient::__setSoapHeaders

(PHP 5 >= 5.0.5, PHP 7, PHP 8)

SoapClient::__setSoapHeaders設定後續呼叫的 SOAP 標頭

說明

public SoapClient::__setSoapHeaders(SoapHeader|陣列|null $headers = null): 布林值

定義要與 SOAP 請求一起傳送的標頭。

注意事項:

呼叫此方法將會取代任何先前的值。

參數

headers (標頭)

要設定的標頭。可以是 SoapHeader 物件或 SoapHeader 物件的陣列。如果未指定或設為 null,則標頭將被刪除。

回傳值

成功時返回 true,失敗時返回 false

範例

範例 #1 SoapClient::__setSoapHeaders() 範例

<?php

$client
= new SoapClient(null, array('location' => "https://127.0.0.1/soap.php",
'uri' => "http://test-uri/"));
$header = new SoapHeader('http://soapinterop.org/echoheader/',
'echoMeStringRequest',
'hello world');

$client->__setSoapHeaders($header);

$client->__soapCall("echoVoid", null);
?>

範例 #2 設定多個標頭

<?php

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

$headers[] = new SoapHeader('http://soapinterop.org/echoheader/',
'echoMeStringRequest',
'hello world');

$headers[] = new SoapHeader('http://soapinterop.org/echoheader/',
'echoMeStringRequest',
'hello world again');

$client->__setSoapHeaders($headers);

$client->__soapCall("echoVoid", null);
?>

新增註釋

使用者貢獻的筆記 3 則筆記

kedar dot purohit @ mavs dot uta dot edu
15 年前
要建立複雜的 SOAP 標頭,您可以執行以下操作:

必要的 SOAP 標頭

<soap:Header>
<RequestorCredentials xmlns="http://namespace.example.com/">
<Token>string</Token>
<Version>string</Version>
<MerchantID>string</MerchantID>
<UserCredentials>
<UserID>string</UserID>
<Password>string</Password>
</UserCredentials>
</RequestorCredentials>
</soap:Header>

對應的 PHP 程式碼

<?php

$ns
= 'http://namespace.example.com/'; //網路服務的命名空間。

//Soap 標頭的內容。
$headerbody = array('Token' => $someToken,
'Version' => $someVersion,
'MerchantID'=>$someMerchantId,
'UserCredentials'=>array('UserID'=>$UserID,
'Password'=>$Pwd));

//建立 Soap 標頭。
$header = new SOAPHeader($ns, 'RequestorCredentials', $headerbody);

//設定 Soap Client 的標頭。
$soap_client->__setSoapHeaders($header);

?>
peamik1953 at NOSPAM dot btinternet dot com
14 年前
您無法新增額外的標頭。如果您想要兩個標頭,且其中一個已存在,請先使用 $client->__setSoapHeaders(NULL) 將其刪除。然後執行 $client->__setSoapHeaders($headers),其中 $headers 是 soapHeader() 物件的陣列。
jayrajput at gmail dot com
15 年前
使用多個 SOAP 標頭時,如果使用 SoapVar 建立 SoapHeader,PHP 程式碼會直接終止(指令終止)。我不確定這是否為錯誤。

沒有 SoapVar 的情況下,程式碼對我來說可以正常運作。

建立 SoapHeader 有不同的方法,我使用的是 SoapVar,但程式碼無法運作。我對 SOAP 的東西仍然是新手。

嘗試使用一般的字串,它可以正常運作。SoapHeader 可以接受 SoapVar 或字串作為第三個參數。

我的程式碼

<?php
// 第一個 SOAP 標頭。
$var = new SoapVar($header, XSD_ANYXML);
$soapHeader = new SoapHeader(NAME_SPACE, "Security", $var);
// 第二個 SOAP 標頭。
$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;

// 這裡我的程式碼就終止了。
$client->__setSoapHeaders($headers);
?>
To Top