NuSOAP 是純 PHP 實現的 SOAP 工具包,無需擴展支援,相容所有 PHP 環境。支援 SOAP 1.1、WSDL 1.1,可與 .NET、Java 等異構系統互操作。
lib/ 目錄置於專案可存取路徑require_once('lib/nusoap.php');<?php
require_once('lib/nusoap.php');
$client = new soapclient('https://localhost/Webservices/Service.asmx?WSDL', 'wsdl');
$params = [
'strUsername' => 'username',
'strPassword' => md5('password')
];
$result = $client->call('login', $params);
// 除錯資訊
// echo $client->debug_str;
?>
<?php
require_once('lib/nusoap.php');
$client = new soapclient('https://localhost/Webservices/Service.asmx?WSDL', 'wsdl');
$proxy = $client->getProxy(); // 產生代理類
$result = $proxy->login('username', md5('password')); // 如呼叫本地方法
?>
核心問題:xml_encoding = 'utf-8' 時,NuSOAP 預設執行 utf8_decode() 導致雙重編碼。
$client->soap_defencoding = 'utf-8';
$client->xml_encoding = 'utf-8';
$client->decode_utf8 = false; // 關鍵:關閉自動解碼
<?php
require_once('lib/nusoap.php');
function hello() {
return 'Hello World!';
}
$server = new soap_server();
$server->register('hello');
$server->service($HTTP_RAW_POST_DATA);
?>
$server->configureWSDL('MyService');
$server->register('concatenate',
['str1' => 'xsd:string', 'str2' => 'xsd:string'], // 輸入
['return' => 'xsd:string'] // 輸出
);
$client->request / $client->response - 原始收發封包$client->debug_str - 詳細除錯追蹤$debug = 1 - 回傳端除錯資訊$err = $client->getError();
if ($err) {
// 記錄錯誤、回傳友善訊息
throw new Exception('WebService 呼叫失敗: ' . $err);
}