PHP Conference Japan 2024

SoapClient 類別

(PHP 5, PHP 7, PHP 8)

簡介

SoapClient 類別為 » SOAP 1.1» SOAP 1.2 伺服器提供客戶端。它可以在 WSDL 或非 WSDL 模式中使用。

類別綱要

class SoapClient {
/* 屬性 */
private ?string $uri = null;
private ?int $style = null;
private ?int $use = null;
private ?string $location = null;
private bool $trace = false;
private ?int $compression = null;
private ?resource $sdl = null;
private ?resource $typemap = null;
private ?resource $httpsocket = null;
private ?resource $httpurl = null;
private ?string $_login = null;
private ?string $_password = null;
private bool $_use_digest = false;
private ?string $_digest = null;
private ?string $_proxy_host = null;
private ?int $_proxy_port = null;
private ?string $_proxy_login = null;
private ?string $_proxy_password = null;
private bool $_exceptions = true;
private ?string $_encoding = null;
private ?array $_classmap = null;
private ?int $_features = null;
private ?resource $_stream_context = null;
private ?string $_user_agent = null;
private bool $_keep_alive = true;
private ?int $_ssl_method = null;
private ?int $_use_proxy = null;
private array $_cookies = [];
private ?array $__default_headers = null;
private ?SoapFault $__soap_fault = null;
private ?string $__last_request = null;
private ?string $__last_response = null;
/* 方法 */
public __construct(?string $wsdl, array $options = [])
public __call(string $name, array $args): mixed
public __doRequest(
    string $request,
    string $location,
    string $action,
    int $version,
    bool $oneWay = false
): ?string
public __getTypes(): ?array
public __setCookie(string $name, ?string $value = null): void
public __setLocation(?string $location = null): ?string
public __soapCall(
    string $name,
    array $args,
    ?array $options = null,
    SoapHeader|array|null $inputHeaders = null,
    array &$outputHeaders = null
): mixed
}

屬性

__default_headers

__last_request

__last_request_headers

__last_response

__last_response_headers

__soap_fault

_classmap

_connection_timeout

_cookies

_digest

_encoding

_exceptions

_features

_keep_alive

_login

_password

_proxy_host

_proxy_login

_proxy_password

_proxy_port

_soap_version

_ssl_method

_stream_context

_use_digest

_use_proxy

_user_agent

compression

httpsocket

httpurl

location

sdl

style

trace

typemap

uri

use

目錄

新增註解

使用者貢獻的註解 9 則註解

hugues at zonereseau dot com
13 年前
當您需要連線到需要傳送額外標頭的服務時,請使用此方法。

以下是如何使用 PHP 和 SoapClient 執行此操作

<?php
class exampleChannelAdvisorAuth
{
public
$DeveloperKey;
public
$Password;

public function
__construct($key, $pass)
{
$this->DeveloperKey = $key;
$this->Password = $pass;
}
}

$devKey = "";
$password = "";
$accountId = "";

// 建立 SoapClient 實例
$url = "";
$client = new SoapClient($url, array("trace" => 1, "exception" => 0));

// 建立標頭
$auth = new ChannelAdvisorAuth($devKey, $password);
$header = new SoapHeader("http://www.example.com/webservices/", "APICredentials", $auth, false);

// 呼叫 wsdl 函式
$result = $client->__soapCall("DeleteMarketplaceAd", array(
"DeleteMarketplaceAd" => array(
"accountID" => $accountId,
"marketplaceAdID" => "9938745" // 廣告 ID
)
),
NULL, $header);

// 顯示結果
echo "<pre>".print_r($result, true)."</pre>";
if(
$result->DeleteMarketplaceAdResult->Status == "Success")
{
echo
"項目已刪除!";
}
?>
spam at kacke dot de
2 年前
如果 soap 回應傳回帶有屬性 xsi:nil="false" 的節點,php-soapclient 目前不會處理它們。

這似乎是一個錯誤(不正確)。

解決方法

class mySoapClient extends SoapClient {
public function __doRequest($request, $location, $action, $version, $one_way = 0) {
$response = parent::__doRequest($request, $location, $action, $version, $one_way);
$response = str_replace('xsi:nil="false"',"",$response);
return $response;
}
}

花了我一天的時間。希望能有所幫助
Toppi
Stefan
10 年前
某些版本的 Xdebug 有一個已知的錯誤,可能會導致 SoapClient 不拋出例外,而是導致嚴重錯誤。

用 xdebug_disable(); 和 xdebug_enable(); 包圍 SoapClient 呼叫,以解決此問題。

參考資料

http://bugs.xdebug.org/view.php?id=249
https://bugs.php.net/bug.php?id=47584
info at nospam x valiton x com
9 年前
注意
我嘗試透過 Proxy 對安全網址發出 fopen 請求時遇到了一些麻煩。我一直從遠端主機收到 400 Bad Request 的回應。它接收 Proxy 網址作為 SNI 主機。為了解決這個問題,我必須明確地將 SNI 主機設定為我嘗試連線的網域。這顯然是此錯誤中概述的問題

https://bugs.php.net/bug.php?id=63519

<?php
$domain
= parse_url($file, PHP_URL_HOST);
$proxy_string = "tcp://" . WP_PROXY_HOST . ":" . WP_PROXY_PORT;
$opts = array(
'http' => array( 'proxy' => $proxy_string ),
'ssl' => array( 'SNI_enabled' => true, 'SNI_server_name' => $domain));
$context = stream_context_create($opts);
$handle = fopen( $file, 'r', false, $context );
?>

src
https://php.dev.org.tw/manual/en/context.http.php#114314
Ricardo Pedrassani
8 年前
如果 XML 在不同層級中有相同名稱的識別碼,則有一個解決方案。您不必提交原始 XML(此 PHP SOAP 物件不允許傳送原始 XML),因此您必須始終將您的 XML 轉換為陣列,如下例所示

$originalXML = "
<xml>
<firstClient>
<name>someone</name>
<adress>R. 1001</adress>
</firstClient>
<secondClient>
<name>another one</name>
<adress></adress>
</secondClient>
</xml>"

//將上面的 XML 轉換為陣列,就像 PHP SOAP 函式要求的那樣
$myParams = array('firstClient' => array('name' => 'someone',
'adress' => 'R. 1001'),
'secondClient' => array('name' => 'another one',
'adress' => ''));

$webService = new SoapClient($someURL);
$result = $webService->someWebServiceFunction($myParams);
peter dot hansen at fastit dot net
15 年前
當您遇到類似以下的錯誤時
"Fatal error: Uncaught SoapFault exception: [HTTP] Error Fetching http headers in"
經過幾次(耗時的)SOAP 呼叫後,請檢查您的網頁伺服器設定。

有時,網頁伺服器的「KeepAlive」設定容易導致此錯誤。對於 SOAP 環境,我建議您停用 KeepAlive。

提示:為您的 SOAP 閘道建立專用的虛擬主機,並僅針對此虛擬主機停用 keepalive 可能很棘手,因為對於普通網頁來說,Keepalive 是一個很好的加速功能。
acopantepuy at gmail dot com
9 年前
當他們想要將變數傳遞到 http 標頭時,就是這樣做的

<?php

$aHTTP
['http']['header'] = "User-Agent: PHP-SOAP/5.5.11\r\n";

$aHTTP['http']['header'].= "username: XXXXXXXXXXX\r\n"."password: XXXXX\r\n";

$context = stream_context_create($aHTTP);

$client=new SoapClient("https://ocppws-cert.extra.bcv.org.ve:443/AltoValor/BancoUniversal?WSDL",array('trace' => 1,"stream_context" => $context));

$result = $client->jornadaActiva();
var_dump($result);
?>
jjlopez
13 年前
如果您在 WSDL 模式下進行 soap 呼叫,並且您的 Web 服務位址包含與 80 不同的埠(例如 http://my_ip_address:8080//service.asmx?wsdl),則會正確擷取 WSDL 檔案,但所有後續請求在主機欄位中都沒有任何埠。當嘗試呼叫任何服務的方法時,這會導致 SoapFault 例外。

您需要重新定義 soapClient 類別並強制在每次呼叫中加入埠。

請參閱此範例

http://www.victorstanciu.ro/php-soapclient-port-bug-workaround/
mcinantyspam at fejm dot pl
8 年前
請注意,如果您提供包含非法 xml 字元(ASCII 代碼 0-8、11-12、14-15 - 或十六進位的 x0-x8、xB-xC、xE-xF)的值,則 php 的 SoapClient 會在請求中傳送它們,儘管此類請求不正確,因為它不符合 XML 1.0 的要求。
您必須始終在將這些字元提供給 SoapClient 之前,在您的資料中取代或移除它們
To Top