首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 相關知識

PHP SOAP 實戰:WSDL 與非 WSDL 兩種模式完整示例

     編輯:PHP 相關知識

PHP SOAP 實戰:WSDL 與非 WSDL 兩種模式完整示例

SOAP 是企業級系統集成的標準協議,PHP 內置 SoapServer/SoapClient 提供完整支持。本文通過「HelloWorld」與「Add」兩個接口,演示基於 WSDL 契約優先與無 WSDL 兩種開發模式的完整代碼。

模式一:基於 WSDL 文件(契約優先,推薦生產環境)

服務端實現(server.php)

<?php
class Service {
    public function HelloWorld() {
        return "Hello";
    }
    public function Add($a, $b) {
        return $a + $b;
    }
}

// 實例化 SoapServer,綁定 WSDL 文件
$server = new SoapServer('soap.wsdl', ['soap_version' => SOAP_1_2]);
$server->setClass("Service");
$server->handle();
?>

WSDL 契約文件(soap.wsdl)

<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions 
    xmlns:soap="https://schemas.xmlsoap.org/wsdl/soap/" 
    xmlns:tns="https://localhost/interface/" 
    xmlns:wsdl="https://schemas.xmlsoap.org/wsdl/" 
    xmlns:xsd="https://www.w3.org/2001/XMLSchema" 
    name="soap" 
    targetNamespace="https://localhost/interface/">
    
  <wsdl:types>
    <xsd:schema targetNamespace="https://localhost/interface/">
      <xsd:element name="HelloWorld">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element name="in" type="xsd:string"/>
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
      <xsd:element name="HelloWorldResponse">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element name="out" type="xsd:string"/>
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
      <xsd:element name="Add">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element name="in" type="xsd:int"/>
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
      <xsd:element name="AddResponse">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element name="out" type="xsd:int"/>
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
    </xsd:schema>
  </wsdl:types>

  <wsdl:message name="AddRequest">
    <wsdl:part name="a" type="xsd:int"/>
    <wsdl:part name="b" type="xsd:int"/>
  </wsdl:message>
  <wsdl:message name="AddResponse">
    <wsdl:part name="c" type="xsd:int"/>
  </wsdl:message>

  <wsdl:portType name="TestSoap">
    <wsdl:operation name="Add">
      <wsdl:input message="tns:AddRequest"/>
      <wsdl:output message="tns:AddResponse"/>
    </wsdl:operation>
  </wsdl:portType>

  <wsdl:binding name="soapSOAP" type="tns:TestSoap">
    <soap:binding transport="https://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="Add">
      <soap:operation soapAction="https://localhost/interface/Add"/>
      <wsdl:input>
        <soap:body use="literal" namespace="https://localhost/interface/"/>
      </wsdl:input>
      <wsdl:output>
        <soap:body use="literal" namespace="https://localhost/interface/"/>
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>

  <wsdl:service name="TestSoap">
    <wsdl:port binding="tns:soapSOAP" name="soapSOAP">
      <soap:address location="https://localhost/interface/myservice.php"/>
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>

客戶端調用(client.php)

<?php
$soap = new SoapClient('https://localhost/interface/soap.wsdl');
echo $soap->Add(1, 2); // 輸出 3
?>

模式二:無 WSDL 文件(快速原型、內部通信)

服務端(server_nowsdl.php)

<?php
class Service {
    public function HelloWorld() { return "Hello"; }
    public function Add($a, $b) { return $a + $b; }
}

// uri 必須與客戶端一致
$server = new SoapServer(null, ['uri' => "abcd"]);
$server->setClass("Service");
$server->handle();
?>

客戶端(client_nowsdl.php)

<?php
try {
    $soap = new SoapClient(null, [
        "location" => "https://localhost/interface/soap.php",
        "uri"      => "abcd",
        "style"    => SOAP_RPC,
        "use"      => SOAP_ENCODED
    ]);
    echo $soap->Add(1, 2);
} catch (Exception $e) {
    echo $e->getMessage();
}
?>

兩種模式對比與選型建議

維度WSDL 模式非 WSDL 模式
契約管理嚴格,支持多語言互操作靈活,僅限 PHP 內部
開發效率需維護 WSDL,初期較慢極快,適合快速迭代
類型安全強類型,編譯期檢查弱類型,運行時發現錯誤
適用場景對外開放 API、異構系統集成內部微服務、快速原型

常見問題排查

  • Windows 下無法使用:確認 php.ini 啟用 extension=soap,PHP 5.0+ 才支持
  • 跨語言調用:如調用 C# ASMX 服務,需注意參數數組格式:['paramName' => 'value']
  • 異常處理:生產環境必須包裹 try-catch,記錄 SoapFault 詳情
  • 性能優化:啟用 WSDL 緩存 ini_set('soap.wsdl_cache_enabled', 1),避免重複解析

SOAP 雖然不如 REST/JSON 輕量,但在強契約、事務、安全性要求高的企業級場景仍是首選。PHP 內置擴展使其開發門檻大幅降低。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved