WebService-php- 2(17)
wsdl实例
<?xml version ='1.0' encoding ='UTF-8' ?>
<definitions
targetNamespace='http://localhost/00/'
xmlns:tns='http://localhost/00/'
xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/'
xmlns:xsd='http://www.w3.org/2001/XMLSchema'
xmlns:soapenc='http://schemas.xmlsoap.org/soap/encoding/'
xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/'
xmlns='http://schemas.xmlsoap.org/wsdl/'>
<!--<types> 元素定义 web service 使用的数据类型,WSDL 使用 XML Schema 语法来定义数据类型,也可以自定义Schema不包含的类型-->
<types>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://localhost/00/">
</xsd:schema>
</types>
<!--
<message> 元素可定义每个消息的部件,以及相关联的数据类型.
-->
<message name='testRequest'>
<part name="term" type="xsd:string"/>
</message>
<message name='testResponse'>
<part name="value" type="xsd:string"/>
</message>
<!--
<portType> 元素是最重要的 WSDL 元素.它可描述一个 web service、可被执行的操作,以及相关的消息.
它告诉你去哪个WebService的连接点,扮演了一个控制者.
-->
<portType name='oplist'>
<operation name='test'>
<input message='tns:testRequest'/>
<output message='tns:testResponse'/>
</operation>
</portType>
<!--<binding> 元素为每个端口定义消息格式和协议细节-->
<binding name='cartSoap' type='tns:oplist'>
<!--style:属性可取值 "rpc" 或 "document",ransport:属性定义了要使用的 SOAP 协议.在这个例子中我们使用 HTTP-->
<soap:binding style='rpc'
transport='http://schemas.xmlsoap.org/soap/http'/>
<!--operation 元素定义了每个端口提供的操作符,对于每个操作,相应的 SOAP 行为都需要被定义-->
<operation name='test'>
<soap:operation soapAction='http://www.cwtservice.cn/newOperation/'/>
<input>
<soap:body use='encoded' namespace='urn:xmethods-delayed-quotes'
encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>
</input>
<output>
<soap:body use='encoded' namespace='urn:xmethods-delayed-quotes'
encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>
</output>
</operation>
</binding>
<!--<service>包含一个或者多个port元素,每个port元素表示一个不同的Web服务-->
<service name='shopWS'>
<port name='cartSoap' binding='tns:cartSoap'>
<soap:address location='http://localhost/00/wss.php'/>
</port>
</service>
</definitions>
Server端示例:
function test($x) {
return $x;
}
$ss = new SoapServer('http://localhost/00/wsdl.xml');
$ss->addFunction('test');
$ss->handle();
Client调用:
$soap = new soapClient('http://localhost/00/wsdl.xml',array('trace'=>true));
var_dump($soap->test(''));
传递和返回数组参数
如果传递或返回的参数为数组,可以在message标签中做说明.
<message name='testRequest'>
<part name="term" type="xsd:ArrayOfString"/>
</message>
<message name='testResponse'>
<part name="value" type="xsd:ArrayOfString"/>
</message>
XML-RPC调用
XML-RPC可以理解为简化版的soap,对数据的包装相对简洁.
php.ini中,要打开extension=php_xmlrpc.dll
/*
求和函数
注意,rpc服务器在调用函数时,传的参数是这样的:
array(0=>'函数名' , 1=>array(实参1,实参2,...实参N) , 2=>NULL)
*/
function hello() {
return 'hello';
}
function sum($method , $args , $extra) {
return array_sum($args);
}
// 创建RPC Server
$server = xmlrpc_server_create ();
xmlrpc_server_register_method ($server , 'hello' , 'hello');
xmlrpc_server_register_method ($server , 'sum' , 'sum');
// 收取请求
$request = $HTTP_RAW_POST_DATA;
//执行调用客户端的XML请求后获取执行结果
$xmlrpc_response = xmlrpc_server_call_method($server, $request , null);
//把函数处理后的结果XML进行输出
header('Content-Type: text/xml');
echo $xmlrpc_response;
//销毁XML-RPC服务器端资源
xmlrpc_server_destroy($server);
客户端:
class rpcclient {
protected $url;
public function __construct($url='' ) {
$this->url = $url;
}
protected function query($request) {
$context = stream_context_create(array('http' => array(
'method' => "POST",
'header' => "Content-Type: text/xml",
'content' => $request
)));
$xml = file_get_contents($this->url, false, $context);
return xmlrpc_decode($xml);
}
public function __call($method , $args) {
$request = xmlrpc_encode_request($method , $args);
return $this->query($request);
}
}
$rpc = new rpcclient('http://localhost/00/rpcs.php');
var_dump($rpc->hello());
var_dump($rpc->sum(,,));
WebService与json Api的区别
WebService json API
数据封装 XML json
复杂度 高 低
底层协议 不限 HTTP
数据类型 可严格定义 不可严格定义
自说明 性自说明 需额外API文档
WebService-php- 2(17)的更多相关文章
- RobotFrameWork webservice soap接口测试 (二)
上一篇提到做soap接口测试自己简单的写了个py,然后就简单的实现了个客户端能对远程接口进行调用,对返回的数据进行解析,可后面想着也觉得不对劲,soap协议虽说不像http协议那么普及,但是现在很多公 ...
- 简述WebService的使用(二)
上集回顾 上一篇我简单的介绍了一下整个WebService建立和后端访问的过程,如果感兴趣可以看一看:简述WebService的使用(一) //如有不懂请留言,觉得有用请点赞 内容提要 这一篇主要介绍 ...
- Web 在线文件管理器学习笔记与总结(17)复制文件 (18)剪切文件
(17)复制文件 ① 复制文件通过copy($src,$dst) 来实现 ② 检测目标目录是否存在,如果存在则继续检测目标目录中是否存在同名文件,如果不存在则复制成功 file.func.php 中添 ...
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(17)-LinQ动态排序
原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(17)-LinQ动态排序 首先修复程序中的一个BUG这个BUG在GridPager类中,把sord修改为s ...
- 从零单排c++ primer(17)
(1)假设一个名字在派生类的作用域内无法正确解析,则编译器将继续在外层的基类作用域中寻找该名字的定义. (2)派生类的成员将隐藏同名的基类成员. (3)除了覆盖继承而来的虚函数之外,派生类最好不要重用 ...
- Windows Phone开发(17):URI映射
原文:Windows Phone开发(17):URI映射 前面在讲述导航的知识,也讲了控件,也讲了资源,样式,模板,相信大家对UI部分的内容应该有了很直观的认识了.那么今天讲什么呢?不知道大家在练习导 ...
- Android菜鸟的成长笔记(17)—— 再看Android中的Unbounded Service
原文:Android菜鸟的成长笔记(17)-- 再看Android中的Unbounded Service 前面已经写过关于startService(Unbounded Service)的一篇文章:&l ...
- Java设计模式(17)解释器模式(Interpreter模式)
Interpreter定义:定义语言的文法,并且建立一个解释器来解释该语言中的句子. Interpreter似乎使用面不是很广,它描述了一个语言解释器是如何构成的,在实际应用中,我们可能很少去构造一个 ...
- python学习笔记(17)--eclipse和pydev的安装及汉化
说明: 1. 本来一直用sublime的REPL跑python,不过在用爬虫下载图片输出页数的时候,由于输出太多行会卡住,而IDLE已经受够了,写起代码来实在是不好用.之前其实也写过一篇文章探讨过各种 ...
- leecode刷题(17)-- 实现StrStr
leecode刷题(17)-- 实现StrStr 实现StrStr 描述: 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串 ...
随机推荐
- javascript instanceof,typeof的区别
区分string 与 String的区别 为什么结果会是false呢? <script type="text/javascript"> var aColors = [& ...
- C++builder XML XSL 代码生成
void __fastcall TFrmGenCode::XSLTxml1Click(TObject *Sender) { // XSLT转换xml文件格式 _di_IXMLDocument xml; ...
- 0 1 1 2 3 5 8 13 21 34 求第N个, 用js实现
function fibo(n) { var f = []; for (var c = 0; c < n; ++c) { console.log(f.join("")) f. ...
- 可视化库-Matplotlib基础设置(第三天)
1.画一个基本的图 import numpy as np import matplotlib.pyplot as plt # 最基本的一个图,"r--" 线条加颜色, 也可以使用l ...
- Oracle Tip: Choosing an efficient design for Boolean column values
Takeaway: When designing a database table structure, it's important to choose an efficient strategy ...
- tomcat 域名直接访问默认工程,而不添加项目路径
<Engine name="Catalina" defaultHost="xx.xx.xx.xx"> <!--For clustering, ...
- python报OperationalError: (1366, "Incorrect string value..."的问题解决
一.环境及问题描述 1. 环境 操作系统:win10,64bit. python版本:2.7.15 mysql版本:5.7.23 2. 问题描述 使用python从某个数据文件读取数据,处理后,用My ...
- [Selenium]Click element under a hidden element
Description: Find out the DDL in Treegrid, but cannot click on it.Because the element is under a hid ...
- Java 设计模式系列(十二)代理模式
Java 设计模式系列(十二)代理模式 代理模式是对象的结构模式.代理模式给某一个对象提供一个代理对象,并由代理对象控制对原对象的引用. package com.github.binarylei.de ...
- line-height:150% 和 line-height:1.5
line-height属性的细节与大多数CSS属性不同,line-height支持属性值设置为无单位的数字.有无单位在子元素继承属性时有微妙的不同. 有单位(包括百分比)与无单位之间的区别有单位时,子 ...