在 PHP 中實現帶 WSDL 的 SOAP

來源:互聯網
上載者:User

標籤:des   style   http   io   ar   os   使用   java   on   

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

    composer require piotrooo/wsdl-creator
  2. 實現用於外部存取的入口檔案,程式碼範例請參考(其中方法名和參數中出現的 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();    }}
  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;    }}
  4. 使用 SoapUI 軟體對介面進行調試

  5. 編製相應的介面調用說明文檔

參考資料:

  • 瞭解 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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.