php xmlrpc的简单实用
来源:互联网 发布:mysql limit where优化 编辑:程序博客网 时间:2024/06/09 16:49
[ Web Service介绍 ]
Web Service就是为了异构系统的通信而产生的,它基本的思想就是使用基于XML的HTTP的远程调用提供一种标准的机制,而省去建立一种新协议的需求。目前进行Web Service通信有两种协议标准,一种是XML-RPC,另外一种是SOAP。XML-RPC比较简单,出现时间比较早,SOAP比较复杂,主要是一些需要稳定、健壮、安全并且复杂交互的时候使用。
PHP中集成了XML-RPC和SOAP两种协议的访问,都是集中在xmlrpc扩展当中。另外,在PHP的PEAR中,不管是PHP 4还是PHP 5,都已经默认集成了XML-RPC扩展,而且该扩展跟xmlrpc扩展无关,能够独立实现XML-RPC的协议交互,如果没有xmlrpc扩展,建议使用PEAR::XML-RPC扩展。
我们这里主要是以XML-RPC来简单描述Web Service的交互过程,部分内容来自PHP手册,更详细内容,建议参考手册。
[ 安装xmlrpc扩展 ]
如果你的系统中没有安装xmlrpc的php扩展,那么请正确安装。在Windows平台下,首先把PHP安装目录下的扩展php_xmlrpc.dll放到C:Windows或者C:Winnt目录下,
(PHP4的扩展在C:phpextensions目录中,PHP5的扩展在C:phpext目录中),同时在
在apache 的安装目录下的php.ini中把extension=php_xmlrpc.dll前面的分号";"去掉,然后重
启Web服务器后查看phpinfo()有没有XML-RPC项目就能够确定是否已经正确安装xmlrpc扩展。
client.php
<?php /** * 函数:提供给客户端进行连接XML-RPC服务器端的函数 * 参数: * $host 需要连接的主机 * $port 连接主机的端口 * $request 封装的XML请求信息 * 返回:连接成功成功返回由服务器端返回的XML信息,失败返回false */function do_call($host, $port, $request) { //打开指定的服务器端 $fp = fsockopen($host, $port, $errno, $errstr); //构造需要进行通信的XML-RPC服务器端的查询POST请求信息 $query = "POST /server.php HTTP/1.0\nUser_Agent: My Egg Client\nHost: ".$host."\nContent-Type: text/xml\nContent-Length: ".strlen($request)."\n\n".$request."\n"; //把构造好的HTTP协议发送给服务器,失败返回false if (!fputs($fp, $query, strlen($query))) { $errstr = "Write error"; return 0; } //获取从服务器端返回的所有信息,包括HTTP头和XML信息 $contents = ''; while (!feof($fp)) { $contents .= fgets($fp); } //关闭连接资源后返回获取的内容 fclose($fp); return $contents; } //构造连接RPC服务器端的信息$host = '127.0.0.1'; $port = 8080; //把需要发送的XML请求进行编码成XML,需要调用的方法是cycle,参数是egg$request = xmlrpc_encode_request('cycle', 'egg'); //调用do_call函数把所有请求发送给XML-RPC服务器端后获取信息$response = do_call($host, $port, $request); //分析从服务器端返回的XML,去掉HTTP头信息,并且把XML转为PHP能识别的字符串$split = '<?xml version="1.0" encoding="iso-8859-1"?>';$xml = explode($split, $response);$xml = $split . array_pop($xml);$response = xmlrpc_decode($xml);//输出从RPC服务器端获取的信息print_r($response);?>server.php
<?php /** * 函数:提供给RPC客户端调用的函数 * 参数: * $method 客户端需要调用的函数 * $params 客户端需要调用的函数的参数数组 * 返回:返回指定调用结果 */function lifecycle($method, $params) { /* $method = 'cycle', $params = (array of) request parameter(s); $data is also passed from xmlrpc_server_call_method, if we had any data to pass */ switch($params[0]) { case 'egg': $reply = 'All eggs will be birds one day.'; break; default: $reply = 'That must have been an otheregg'; } return $reply; } //产生一个XML-RPC的服务器端$server = xmlrpc_server_create(); /* register the 'external' name and then the 'internal' name */ xmlrpc_server_register_method($server, "cycle", "lifecycle"); $request = $HTTP_RAW_POST_DATA; // no you don't need 'always on', and no $_POST doesn't work. /* the parameters here are 'server, xml-string and user data'. There's supposed to be an optional 'output options' array too, but I can't get it working :( hence header() call */ $response = xmlrpc_server_call_method($server, $request, null); header('Content-Type: text/xml'); print $response; //销毁XML-RPC服务器端资源 xmlrpc_server_destroy($server); ?>
运行输出:All eggs will be birds one day.
转载于:http://blog.chinaunix.net/uid-25979788-id-3303913.html
- php xmlrpc的简单实用
- 简单的java xmlrpc
- servlet与php的xmlrpc
- php---xmlrpc
- php的web service实现----xmlrpc
- 简单实用的分页函数(Php+mysql)
- 简单实用的PHP验证码类
- php生成xml的简单实用
- 简单实用的PHP文本缓存类
- 简单实用的php爬虫系统
- 一个php简单实用的分页函数
- 简单实用的PHP分页代码
- 几个简单又实用的PHP函数
- 简单实用的 PHP、数组,对象 互转
- PHP XMLRPC 实践
- XMLRPC example in PHP
- PHP XMLRPC 使用示例
- xmlrpc.php brute attack
- OpenGL着色器(二)
- shoelace源代码分析之bootlace
- CodeForces Good Bye 2013
- 服务器-apache-common-httpclient测试
- HTML5实战与剖析之自定义数据属性(dataset)
- php xmlrpc的简单实用
- 电子商务马志伟C#实验报告
- js 读取cookie 添加 删除
- 加载性能优化ViewHolder
- Outdoor Light Scattering From Intel
- 计算阶乘n!末尾所含0的个数
- cookie.js js操作cookie
- Spring与Struts配置文件笔记
- php禁止浏览器直接访问