標籤:des style http io ar os 使用 java on
-
用 composer 安裝產生 WSDL 所需的庫
composer require piotrooo/wsdl-creator
-
實現用於外部存取的入口檔案,程式碼範例請參考(其中方法名和參數中出現的 Notify 對應一個類名,該類的方法將成為可以通過 SOAP 調用的外部介面):
<?phpuse 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(); }}
-
實現包含介面功能邏輯的類檔案,程式碼範例請參考(其中方法的注釋相當重要,是 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; }}
-
使用 SoapUI 軟體對介面進行調試
-
編製相應的介面調用說明文檔
參考資料:
- 瞭解 SOAP 的基本文檔結構請參考 http://www.sitepoint.com/series/creating-web-services-with-php-and-soap/]
- 關於產生 WSDL 的庫 wsdl-creator 詳見: https://github.com/piotrooo/wsdl-creator
在 PHP 中實現帶 WSDL 的 SOAP