MICUU
心情
所有
图集
登录
搜索
原创
WebService中的WSDL讲解
半字浅眉-
发布于:2021-05-20
最近调用接口,接触到了webservice接口!一开始很蒙蔽的,知道现在也不是特别清晰!别人就给了一个接口,需要我们对接,只有各种百度。 下面就我目前的认知来给大家做一下简单的讲解。 给的WSDL地址我们直接放到地址栏,会出现xml格式的代码,下面做简单分析。 > 首先 一个WSDL文档通常包含8个重要的元素, 即definitions、types、import、message、portType、operation、binding、service元素。这些元素嵌套在definitions元素中,definitions是WSDL文档的根元素 > 下图一定要仔细看 [](http://micuer.com/data/upload/20210520/60a60321e46eb.png) 下面是简单的PHP调用实例: $url = 'http://webdmstest.bydauto.com.cn:81/DCS_TEST/services/DMS_DFH_02?wsdl'; $param["roNo"] = 9999999; $wsdl = $url; $client = new WebServer($wsdl, ['encoding'=>'UTF-8']); $result = $client->callSoapFun('getRepairOrderStatus', $param); p($result); //getRepairOrderStatus 上图中对应方法的名称 其中WebServer类的定义 ``` <?php namespace App\Common; use SoapClient; /** * PHP调用webServer接口DEMO * 以调用天气预报为例 * * WS请求公共类 * 同时支持WSDL和non-WSDL两种模式 */ class WebServer { //WS请求地址 protected $url; //WS实例参数 //这里参数可以根据实际需求添加,如加密令牌、ssl、http等参数 protected $options; //WS头参数 protected $headers; protected $client; public function __construct($url = null, $options = [], $headers = []) { $this->url = $url; $this->options = $options; $this->headers = $headers; $this->createSoapClient(); } public function createSoapClient(){ //non-WSDL模式参数location(发送请求的SOAP服务器的URL)和uri(SOAP服务的目标名称空间)为必填 if (!$this->url && (!$this->options['location'] || !$this->options['uri'])) { return false; } //设置代理请求WS // $this->options = [ // 'proxy_host' => 'ssssss.sssssss.com', //不需要添加http或https // 'proxy_port' => '8080', // 'proxy_login' => '开机账号', // 'proxy_password' => '开机密码' // ]; // $this->options = [ // 'proxy_host' => 'ss.ss.ss.ss', //不需要添加http或https // 'proxy_port' => '8080', // 'proxy_login' => 'ke.zengli', // 'proxy_password' => 'Hanyu12345' // ]; try { $this->client = new SoapClient($this->url, $this->options); if ($this->client) { return $this->client; } else { return false; } } catch (\SoapFault $e){ file_put_contents('./log.txt', $e->getMessage()); return false; } catch (\Exception $e) { file_put_contents('./log.txt', $e->getMessage()); return false; } } /** * 设置WS头 */ public function setSoapHeaders(){ if (count($this->headers)) { $this->client->__setSoapHeaders($this->headers); } } /** * 获取WS中可以使用的方法和参数 * 在调用前,可以使用getFunctions和getTypes看一下该接口暴露的方法、参数和数据类型 * 这里需要注意的是传入的参数名一定要和soapclient里面定义的一致,否则参数是传不过去的 */ public function getWsFunAndType(){ if(!$this->client) return false; $functions = $this->client->__getFunctions(); $types = $this->client->__getTypes(); return ['functions' => $functions, 'types' => $types]; } /** * 请求WS * * @param string $method 待请求WS方法 * @param array|string $data 待发送数据,['parm1'=>'参数1', 'parm2'=>'参数2', 'parm3'=>'参数3', ……] * @return boolean 是否成功 */ public function callSoapFun($method, $data) { if (!$method) return false; $this->setSoapHeaders(); try{ $responseRes = $this->client->__soapCall($method, [$data]); } catch (\SoapFault $e){ return false; } catch (\Exception $e) { return false; } catch (\Throwable $e) { //Throwable 在 PHP 7 中可以用作任何对象抛出声明的基本接口,包括 Expection (异常)和 Error (错误) return false; } if (is_object($responseRes)) { $responseRes = $this->objectToArray($responseRes); } return $responseRes ? $responseRes : false; } /** * 将stdClass Object转换成array格式 * @param $array 需要转换的对象 * @return array */ public function objectToArray($array) { if(is_object($array)) { $array = (array)$array; } if(is_array($array)) { foreach($array as $key=>$value) { $array[$key] = $this->objectToArray($value); } } return $array; } } // // $wsdl = 'http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl'; // $client = new WebServer($wsdl, ['encoding'=>'UTF-8']); // $result = $client->callSoapFun('getWeatherbyCityName', ['theCityName'=>'天津']); // print_r($result); ``` 具体详细的协议及节点介绍请参考: https://blog.csdn.net/wenzhi20102321/article/details/68486526
注:原创不易,转载请注明出处(
https://micuu.com/new/1574.html
),本站所有资源来源于网络收集,如有侵权请联系QQ245557979进行清除。
最后修改与 2022-02-22
上一篇:
PHP开发编码规范-命名、数据表、字段
下一篇:
php如何查看webservice - WSDL链接中可用的方法
留言反馈
请先登录
问题反馈渠道,如有软件无法下载或者其他问题可反馈。【由于某种原因,目前留言不展示】
用户需要登录后才能留言反馈
立即留言
珍藏视频
10分钟高效燃脂
30天高效瘦脸操
5分钟缓解颈椎操
友人
微博
全民K歌
唱吧
今日头条
悠悠网
科技小锅盖
彼岸桌面
阮一峰
laravel社区
V2ex
掘金
更多>