在 PHP 中實現帶 WSDL 的 SOAP

jopen 10年前發布 | 20K 次閱讀 SOAP PHP開發

  1. 用 composer 安裝生成 WSDL 所需的庫

    composer require piotrooo/wsdl-creator
    </li>

  2. 實現用于外部訪問的入口文件,代碼示例請參考(其中方法名和參數中出現的 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>

  3. 實現包含接口功能邏輯的類文件,代碼示例請參考(其中方法的注釋相當重要,是 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>

  4. 使用 SoapUI 軟件對接口進行調試

    </li>

  5. 編制相應的接口調用說明文檔

    </li> </ol>

    參考資料:

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!