用 PHP 實作 Web Service-一次搞懂 XML、SOAP、WSDL (下)

by Benmr

實作

有了上文的名詞解釋
我們總算可以開始來實作了
以下將以 PHP 的 SoapClient 為例
說明怎麼用這個 class 來對 server 端發送 request 做 XML 格式的資料交換

Contruct

根據官方說明
(https://www.php.net/manual/en/soapclient.construct.php)
SoapClient::__construct 可以是 WSDL 或 non-WSDL mode 的
以下將以 WSDL 為例

官方裡有提到

public SoapClient::__construct(?string $wsdl, array $options = [])

可以到有兩個要素: WSDL 和 options

WSDL

就是 WSDL 的位置
可能放在遠端也可能掛在 local 內

  • 若放在遠端

    • 放遠端的 URL
    • 例: https://domain.net/example?WSDL
      (注意這個最後的 ?WSDL 很重要 server 端的 url 結尾不是 .wsdl 的話
      就得改成這樣 原因不明
      我之前就是沒放所以 GG 了)
  • 若放在 local

    • 放你 local 的絕對路徑
    • 例: /your/path/to/file.xml

options

這個選項就多了
比較重要的應該是 stream_contexttrace
這邊就可以設定一下 SSL 協定
例:

$streamContext = [
            "ssl" => [
                "verify_peer" => false,
                "verify_peer_name" => false
            ]
        ];
// 使用 `stream_context_create` 這個 func 來建 streamContext 物件,trace 開起來後有利之後看 log        
$client = new SoapClient($url, ['stream_context' => stream_context_create($streamContext), 
                                'trace' => 1]);        

註: 如果在發送 SOAP 時也要在 http header 塞一些東西 (比如說 token 之類的)
也要特別寫在 stream_context 裡面

所以上面的例子可改寫成

$httpHeader = 'token: 123456';
$streamContext = [
            "ssl" => [
                "verify_peer" => false,
                "verify_peer_name" => false
            ],
            // 把這個塞進來,其他的不變
            'http' => [
                'header' => $httpHeader, 
            ]
        ];
$client = new SoapClient($url, ['stream_context' => stream_context_create($streamContext), 
                                'trace' => 1]);        

setSoapHeaders

SOAP 當然也有自己的 header 要處理
所以會使用到這個 func

例:

$auth = array(
        'UserName'=>'USERNAME',
        'Password'=>'PASSWORD',
        'SystemId'=> array('_'=>'DATA','Param'=>'PARAM'),
        );

呼叫 function

最後當然就是呼叫 WSDL 裡定義的 func 來 call SOAP 啦
這邊就是根據文件或 WSDL 裡寫的 func 來做呼叫

所以整段code 的樣貌會變成:

// 宣告目的 url
$url = 'https://domain.net/example?WSDL';

// 宣告 httpHeader,並塞到 streamContext 裡
$httpHeader = 'token: 123456';
$streamContext = [
            "ssl" => [
                "verify_peer" => false,
                "verify_peer_name" => false
            ],
            // 把這個塞進來,其他的不變
            'http' => [
                'header' => $httpHeader, 
            ]
        ];
// 使用上述的資料 new 一個 SoapClient 出來        
$client = new SoapClient($url, ['stream_context' => stream_context_create($streamContext), 
                                'trace' => 1]);   

// 在 soapHeader 裡加入需要的資料
$auth = array(
        'UserName'=>'USERNAME',
        'Password'=>'PASSWORD',
        'SystemId'=> array('_'=>'DATA','Param'=>'PARAM'),
        );
$header = new SoapHeader('NAMESPACE','Auth', $auth, false);
$client->__setSoapHeaders($header);

// 假設在 body 裡面是被說明這次呼叫的目的
$soapBody = [
    'action' => 'login'
];

// 根據文件以及當下的情境,我們要去執行 SOAP 裡面的 login 這個 func
$function = 'login';

// 執行這個 func 同時使用剛宣告的 soapBody
$response = $client->function($soapBody);

以上這樣
就完成了一個簡單的 SOAP 呼叫了

如果有任何問題 歡迎留言詢問
希望這篇文章有幫助到你~!

參考資料

http://yes.nctu.edu.tw/Lecture/NewTech/C05/WebServices/Web%20Services%E4%BB%8B%E7%B4%B9.htm
https://dotblogs.com.tw/marcus116/2011/08/28/34524
https://morosedog.gitlab.io/technology-20200226-tech-3/
https://www.w3schools.com/xml/xml_soap.asp
https://www.rusnake.com/2016/08/12/php-soapclient-%E9%80%A3%E6%8E%A5-https-%E7%9A%84-webservice/
https://www.php.net/manual/en/class.soapclient.php
https://zh.wikipedia.org/wiki/Web%E6%9C%8D%E5%8A%A1

You may also like

Leave a Comment