• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

在 PHP 中实现带 WSDL 的 SOAP

php 搞代码 3年前 (2022-01-23) 16次浏览 已收录 0个评论

用 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-a来@源gao*daima.com搞@代#码网搞gaodaima代码nd-soap/] 关于生成 WSDL 的库 wsdl-creator 详见: https://github.com/piotrooo/wsdl-creator


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:在 PHP 中实现带 WSDL 的 SOAP
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址