查詢

SoapClient::__doRequest()函式—用法及示例

「 執行SOAP請求 」


函式名:SoapClient::__doRequest()

適用版本:PHP 5, PHP 7

用法: SoapClient::__doRequest(string $request, string $location, string $action, int $version [, int $one_way = 0])

引數:

  • $request:包含要傳送的SOAP請求的字串。
  • $location:SOAP伺服器的URL。
  • $action:SOAP操作的操作標頭。
  • $version:SOAP協議版本(1表示SOAP 1.1,2表示SOAP 1.2)。
  • $one_way(可選):如果設定為1,則該方法將不會等待伺服器的響應。

返回值: 返回包含伺服器響應的字串。

示例:

$wsdl = 'http://example.com/soap.wsdl';
$options = array(
    'trace' => true,
    'exceptions' => true,
    'cache_wsdl' => WSDL_CACHE_NONE
);
$client = new SoapClient($wsdl, $options);

$request = '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
               <soap:Body>
                   <getWeather>
                       <city>London</city>
                   </getWeather>
               </soap:Body>
           </soap:Envelope>';

$location = 'http://example.com/soap-server.php';
$action = 'http://example.com/soap-action';

$response = $client->__doRequest($request, $location, $action, SOAP_1_2);

echo $response;

上述示例中,我們首先建立了一個SoapClient物件,並指定了相關的選項。然後,我們定義了要傳送的SOAP請求的XML字串,並將SOAP伺服器的URL、SOAP操作的操作標頭和SOAP協議版本傳遞給__doRequest()方法。最後,我們透過echo語句輸出伺服器的響應。

補充糾錯
熱門PHP函式
分享連結