当前位置:首页 > 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实现文件上传

php实现文件上传

PHP 文件上传实现方法 在 PHP 中实现文件上传需要处理 HTML 表单和服务器端脚本。以下是详细步骤: 创建 HTML 表单 使用 enctype="multipart/form-data"…

php实现树

php实现树

PHP实现树结构的方法 在PHP中实现树结构通常可以通过递归或迭代的方式完成。以下是几种常见的实现方法: 递归实现树结构 递归是处理树结构的自然方式,尤其适用于具有未知深度的树。 class Tr…

php 实现自动收货

php 实现自动收货

PHP 实现自动收货功能 自动收货功能通常用于电商系统,在订单超过一定时间未手动确认收货时自动完成订单。以下是实现方法: 数据库设计 需要订单表包含以下关键字段: CREATE TABLE o…

php实现

php实现

PHP 实现的基本方法 PHP 是一种服务器端脚本语言,广泛用于 Web 开发。以下是 PHP 实现的一些常见方法。 变量与数据类型 PHP 变量以 $ 开头,支持多种数据类型: $nam…

php 购物车实现

php 购物车实现

数据库设计 购物车功能通常需要设计数据库表存储商品和用户信息。常见的表包括products(商品表)、users(用户表)和cart(购物车表)。cart表通常包含字段:id(主键)、user_id(…

php 实现购物车

php 实现购物车

PHP 实现购物车功能 购物车功能是电子商务网站的核心部分,可以通过 PHP 和 Session 或数据库来实现。以下是两种常见的实现方式: 使用 Session 存储购物车数据 Sessi…