PHP 需啟用三個擴展(修改 php.ini 並重啟 Web Server):
extension=php_soap.dll
extension=php_curl.dll
extension=php_openssl.dll
在 web.config 的 <system.web> 區段加入協定支援:
<webServices>
<protocols>
<add name="HttpSoap"/>
<add name="HttpPost"/>
<add name="HttpGet"/>
<add name="Documentation"/>
</protocols>
</webServices>
WebMethod 範例:
[WebMethod(Description = "This......", EnableSession = false)]
public string ChkWele(string username, string psw) {
string ret = "";
// 業務邏輯
return ret;
}
<?php
header("content-type:text/html;charset=utf-8");
// 1. 建立 SoapClient,傳入 WSDL 網址
$client = new SoapClient("https://192.168.3.178:8080/ChkWelePsw.asmx?WSDL");
// 2. 封裝參數(關聯陣列)
$param = array('username' => 'test3', 'psw' => '123');
// 3. 呼叫 __soapCall 方法
// 參數1:方法名稱;參數2:陣列,key 必須為 'parameters'
$p = $client->__soapCall('ChkWele', array('parameters' => $param));
// 4. 取得結果:.NET 回傳會自動產生 方法名+Result 屬性
print_r($p->ChkWeleResult);
?>
?WSDL 結尾__soapCall(),不可直接 $client->ChkWele()方法名Result 屬性(如 ChkWeleResult)| 錯誤現象 | 可能原因 | 解決方向 |
|---|---|---|
| Class 'SoapClient' not found | 未啟用 php_soap.dll | 檢查 php.ini、重啟 Apache/IIS |
| SOAP-ERROR: Parsing WSDL | WSDL 網址錯誤/防火牆阻擋 | 瀏覽器直接開啟 WSDL 驗證、檢查網路 |
| Object doesn't contain property | 參數名稱不匹配 | 對照 .NET 方法簽名、大小寫敏感 |
| 中文亂碼 | 編碼不一致 | 雙端統一 UTF-8、必要時 urlencode |
若無 WSDL,可手動指定 URI、Location、Style:
$client = new SoapClient(null, array(
'location' => "https://example.com/service.asmx",
'uri' => "https://tempuri.org/",
'style' => SOAP_RPC,
'use' => SOAP_ENCODED
));