首页 > 编程语言 >WebService-php- 2(17)

WebService-php- 2(17)

时间:2022-11-30 12:03:48浏览次数:36  
标签:http WebService xmlrpc server xmlsoap 17 org php soap

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('10086'));

传递和返回数组参数
如果传递或返回的参数为数组,可以在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(4,5,6));

WebService与json Api的区别

      WebService   json API
数据封装  XML       json
复杂度   高        低
底层协议     不限        HTTP
数据类型     可严格定义    不可严格定义
自说明        性自说明    需额外API文档

源码面前,了无秘密



标签:http,WebService,xmlrpc,server,xmlsoap,17,org,php,soap
From: https://blog.51cto.com/zhenghongxin/5898207

相关文章

  • 在线编辑器Ckeditor (1) - php (30)
    在线编辑器在线编辑器也称之为所见即所得编辑器,是一种常见的html源码编辑器。所见即所得:用户在输入的时候,不论是格式和是样式都能被系统原封不动的保存,最后在查看的时候,可以......
  • 在线编辑器Ckeditor (2) - php (31)
    接上一篇3in-page(页内)配置,在使用Ckeditor的界面里进行直接配置页内配置效果 特点:配置项完全属于某个特定的Ckeditor实例,不可重用三种配置方式比较定制方式特点说明优先......
  • PHP设计超级好用的文件上传处理类一 (37)
    <?phpclassFileUpload{private$filepath;//指定上传文件保存的路径private$allowtype=array('gif','jpg','png','jpeg');//充许上传文件......
  • PHP设计日历类一 (38)
    由两个文件组成:第一个test.php<style>table{border:1pxsolid#050;}.fontb{color:white;background:blue;}th{......
  • Thinkphp入门 四 —布局、缓存、系统变量 (48)
    【控制器操作方法参数设置】​​http://网址/index.php/控制器/操作方法​​  【页面跳转】【变量调节器】Smarty变量调节器TP变量调节器:普通的php函数(count strlen ......
  • 十二生肖查询网页版制作(php)
    今天无聊做了一个十二生肖查询器:预览网址效果:​​http://hongxing01.hktd02u.me48.com/03Sxcx​​源代码下载:​​http://down.51cto.com/data/1985014​​这个Demo的学习很......
  • 【开发小技巧】02-如何使用canvasJS在PHP中制作动态图表?
    来源| https://www.geeksforgeeks.org/how-to-make-dynamic-chart-in-php-using-canvasjs/CanvasJS是一个JavaScript库,用于轻松为网页创建其他类型的图表。例如条形图,饼图......
  • PHP获取今天,昨天,本月,上个月,本年 起始时间戳
    https://cloud.tencent.com/developer/article/1885951?from=15425date_default_timezone_set("Asia/Shanghai");//设置为上海时间否则开始时间会相差8个小时//获取......
  • 题解 CF1743B
    这个题是个简单的构造题因为不能有连续的“排列”,而排列序列都是必须是以\(1\)开头所以我们只要让\(2\)和\(1\)不相邻就能保证一个序列里只有它本身和\(1\)这两......
  • 题解 CF1719B
    题解CF1719B这个题观察样例,可以发现,被选中的两个数,一定是相邻的两个数。所以,我们只需要先循环一遍,看看有多少数满足,然后判断是否等于n。如果等于说明可以,先输出YES......