PHP 对接 wsdl soap 接口

PHP 对接 wsdl soap 接口


2020-07-01

概念

SOAP

WSDL

WSDL 描述了业务方法,各语言基本都提供了工具可以自动生成代码,可以直接调用;对接就变得非常简单,拢共就2步骤:

  1. 自动生成接口代码
  2. 封装 service 提供给业务调用

1. 自动生成接口代码

PHP 要处理 SOAP,可以使用这个库,https://github.com/phpro/soap-client

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
    $ composer require phpro/soap-client
    
    $ ./vendor/bin/soap-client wizard
    
     Where would you like to store your config file? [config/soap-client.php]:
     > common/config/soap-client.php
    
    
     Wsdl location (URL or path to file):
     > common/config/standard-prod.wsdl
    
    
     Generic name used to name this client (Results in <name>Client <name>Classmap etc.):
     > GoogleMap
    
     Directory where the client should be generated in:
     > common/libs/googlemap
    
     Namespace for your client:
     > common\libs\googlemap
    
    
     [OK] Config has been written to common/config/soap-client.php
     Generated class common\libs\googlemap\Type\PingRequest to common/libs/googlemap/Type/PingRequest.php
     ...
    
     [OK] All SOAP types generated                                                                                          
                                                                                                                            
     [OK] Generated classmap at common/libs/googlemap/GoogleMapClassmap.php                                                     
                                                                                                                            
     [OK] Generated client at common/libs/googlemap/GoogleMapClient.php                                                         
                                                                                                                            
     [OK] Generated client factory at common/libs/googlemap/GoogleMapClientFactory.php
    
    

2. 封装 service 提供给业务调用

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
    <?php
    
    namespace service\supplier\googlemap;
    
    use common\libs\googlemap\GooglaMapClientFactory;
    
    class MapService
    {
        protected $client;
    
        public function __construct()
        {
            $this->client = GooglaMapClientFactory::factory("/path/to/config/standard-prod.wsdl");
        }
    
        // 调用 soap 实际业务方法
        public function getA()
        {
            return $this->client->getA();
    		}
    
    }

写在结尾

越来越多接口提供商都不再使用 wsdl / soap 对外服务了,性能很差,我对接过的服务商基本都改成轻量的 json 格式,甚至改成了 grpc

沪ICP备2022013452号-1