当前位置:首页 > PHP

php soap wsdl实现

2026-02-16 08:17:26PHP

PHP SOAP WSDL 实现

创建 WSDL 文件

WSDL(Web Services Description Language)是描述 Web 服务的 XML 格式文件。以下是一个简单的 WSDL 示例,定义了一个名为 HelloService 的服务,包含一个 sayHello 操作:

<?xml version="1.0" encoding="UTF-8"?>
<definitions name="HelloService"
    targetNamespace="http://example.com/hello"
    xmlns="http://schemas.xmlsoap.org/wsdl/"
    xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
    xmlns:tns="http://example.com/hello"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">

    <message name="sayHelloRequest">
        <part name="name" type="xsd:string"/>
    </message>
    <message name="sayHelloResponse">
        <part name="greeting" type="xsd:string"/>
    </message>

    <portType name="HelloPortType">
        <operation name="sayHello">
            <input message="tns:sayHelloRequest"/>
            <output message="tns:sayHelloResponse"/>
        </operation>
    </portType>

    <binding name="HelloBinding" type="tns:HelloPortType">
        <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
        <operation name="sayHello">
            <soap:operation soapAction="sayHello"/>
            <input>
                <soap:body use="literal"/>
            </input>
            <output>
                <soap:body use="literal"/>
            </output>
        </operation>
    </binding>

    <service name="HelloService">
        <port name="HelloPort" binding="tns:HelloBinding">
            <soap:address location="http://example.com/hello.php"/>
        </port>
    </service>
</definitions>

实现 SOAP 服务端

在 PHP 中,可以使用 SoapServer 类来实现 SOAP 服务端。以下是一个简单的服务端实现:

php soap wsdl实现

<?php
// 指定 WSDL 文件路径(非必须,但可用于自动生成 WSDL)
$wsdl = "hello.wsdl";

// 创建 SoapServer 实例
$server = new SoapServer($wsdl);

// 添加可调用的函数或类
$server->addFunction("sayHello");

// 定义 sayHello 函数
function sayHello($name) {
    return "Hello, " . $name . "!";
}

// 处理 SOAP 请求
$server->handle();
?>

创建 SOAP 客户端

使用 SoapClient 类可以轻松调用 SOAP 服务。以下是一个客户端示例:

php soap wsdl实现

<?php
// WSDL 文件路径或服务端 URL
$wsdl = "http://example.com/hello.php?wsdl";

// 创建 SoapClient 实例
$client = new SoapClient($wsdl);

// 调用远程方法
$response = $client->sayHello(array("name" => "World"));

// 输出响应
echo $response->greeting; // 输出: Hello, World!
?>

使用类作为服务端

可以将一个类的方法暴露为 SOAP 服务:

<?php
class HelloService {
    public function sayHello($name) {
        return "Hello, " . $name . "!";
    }
}

$wsdl = "hello.wsdl";
$server = new SoapServer($wsdl);
$server->setClass("HelloService");
$server->handle();
?>

调试与错误处理

SOAP 调用可能会抛出异常,可以通过 try-catch 捕获:

try {
    $client = new SoapClient($wsdl);
    $response = $client->sayHello(array("name" => "World"));
    echo $response->greeting;
} catch (SoapFault $e) {
    echo "SOAP Fault: " . $e->getMessage();
}

注意事项

  • WSDL 文件必须正确描述服务端的方法和参数。
  • 确保 PHP 的 SOAP 扩展已启用(extension=soap)。
  • 对于复杂类型,需要在 WSDL 中定义相应的 XML Schema。

以上示例展示了如何使用 PHP 实现基于 WSDL 的 SOAP 服务端和客户端。

标签: phpsoap
分享给朋友:

相关文章

php实现mysql

php实现mysql

PHP 连接 MySQL 的基本方法 PHP 提供了多种方式与 MySQL 数据库进行交互,以下是最常用的几种方法。 使用 MySQLi 扩展 MySQLi 是 MySQL 的改进扩展,支持面向对…

php队列的实现

php队列的实现

PHP队列的实现方式 PHP中实现队列功能可以通过多种方式,根据需求选择适合的方案: 使用数组实现基础队列 PHP数组本身支持队列操作,利用array_push和array_shift函数: $q…

php 实现排序

php 实现排序

PHP 实现数组排序的方法 PHP 提供了多种内置函数和方法来实现数组排序,适用于不同的排序需求。以下是一些常见的排序实现方式: 使用 sort() 函数对数组进行升序排序 sort() 函数对数组…

php实现短信验证

php实现短信验证

PHP实现短信验证的步骤 选择合适的短信服务提供商 国内常见的短信服务提供商包括阿里云短信、腾讯云短信、云片等。注册账号并获取API密钥和签名模板是第一步。这些平台通常提供详细的开发文档和SDK。…

php实现评论功能

php实现评论功能

数据库设计 创建评论表(示例结构): CREATE TABLE `comments` ( `id` int(11) NOT NULL AUTO_INCREMENT, `user_id`…

php下载文件实现

php下载文件实现

PHP 下载文件实现方法 使用 header() 函数强制下载 设置合适的 HTTP 头信息,强制浏览器下载文件而非直接打开。 $file_path = '/path/to/file.pdf'; $…