在 PHP 中實現帶 WSDL 的 SOAP
-
用 composer 安裝生成 WSDL 所需的庫
composer require piotrooo/wsdl-creator
</li> -
實現用于外部訪問的入口文件,代碼示例請參考(其中方法名和參數中出現的
Notify
對應一個類名,該類的方法將成為可以通過 SOAP 調用的外部接口):<?php use WSDL\DocumentLiteralWrapper; use WSDL\WSDLCreator; use WSDL\XML\Styles\DocumentLiteralWrapped;
class Api { public static function soapNotify() { $host = $_SERVER['HTTP_HOST']; $soapuri = "http://{$host}/Api/soapNotify"; if (isset($_GET['wsdl'])) { $wsdl = new WSDLCreator('Notify', $soapuri); $wsdl->renderWSDL(); exit; }
$server = new SoapServer(null, [ 'uri' => $soapuri ]); $server->setClass('Notify'); $server->handle(); }
}</pre></li>
-
實現包含接口功能邏輯的類文件,代碼示例請參考(其中方法的注釋相當重要,是 wsdl-creator 正確生成 WSDL 的依據,必須嚴格按照格式進行注釋):
class Notify { /**
* @desc sendText 向患者的微信發送文本信息 * @param string $toUser 發給哪個用戶 * @param string $content 發送的內容 * @return string $result */ public function sendText($toUser, $content) { if (!$toUser || !$content) { return E::INVALID; } $user = G::xpdo()->row("SELECT * FROM `users` WHERE `patientid` = ?", [$toUser]); if (!$user) { return E::NODATA; } $api = G::xpdo()->row("SELECT * FROM `api` WHERE `token` = ?", [$user['token']]); $weixin = new Weixin($api['appid'], $api['appsecret']); $weixin->sendText($user['openid'], $content); return 0; }
}</pre></li>
-
使用 SoapUI 軟件對接口進行調試
</li> -
編制相應的接口調用說明文檔
</li> </ol>參考資料:
- 了解 SOAP 的基本文檔結構請參考 http://www.sitepoint.com/series/creating-web-services-with-php-and-soap/]
- 關于生成 WSDL 的庫 wsdl-creator 詳見: https://github.com/piotrooo/wsdl-creator
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!