You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现带有多个Base URL的SOAP API客户端?

构建多Base URL的SOAP API客户端

因为这个API拆分了读取和写入操作的WSDL与Base URL,你需要在客户端中维护两个独立的SoapClient实例,分别对应读取(GET)和写入(POST)操作。下面是具体实现方式:

use SoapClient;

class ExampleApiClient
{
    protected $readClient;
    protected $writeClient;

    public function __construct()
    {
        // 初始化读取操作的SOAP客户端(对应GET的WSDL)
        $this->readClient = new SoapClient('https://www.example.com/webservices3/ExampleWS.asmx?wsdl', [
            // 可添加通用配置,比如WSDL缓存、超时等
            'cache_wsdl' => WSDL_CACHE_BOTH,
            'connection_timeout' => 10,
        ]);

        // 初始化写入操作的SOAP客户端(对应POST的WSDL)
        $this->writeClient = new SoapClient('https://www.example.com/webservices3/ExampleWSPost.asmx?wsdl', [
            'cache_wsdl' => WSDL_CACHE_BOTH,
            'connection_timeout' => 10,
        ]);
    }

    // 读取类操作:使用readClient
    public function getUser($userId)
    {
        return $this->readClient->soapCall('GetUser', ['userId' => $userId]);
    }

    // 写入类操作:使用writeClient
    public function addUser($userData)
    {
        return $this->writeClient->soapCall('AddUser', $userData);
    }
}

// 调用示例
$api = new ExampleApiClient();
$user = $api->getUser(12);
$newUser = $api->addUser(['name' => '张三', 'email' => 'zhangsan@example.com']);

关键说明

  • 拆分两个客户端实例:严格区分读取和写入操作对应的WSDL,避免混淆操作与地址的对应关系
  • 统一配置抽离(可选):如果两个客户端的配置项大部分相同,可以把配置抽成私有方法,减少重复代码
  • 方法职责单一:每个业务方法明确对应到正确的客户端,保证请求发送到正确的Base URL

内容的提问来源于stack exchange,提问作者qwaz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 00:25:26