php xmlrpc的简单实用
2013-12-31 13:09
357 查看
[ 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
运行输出:All eggs will be birds one day.
转载于:http://blog.chinaunix.net/uid-25979788-id-3303913.html
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实现简单实用的验证码类
- PHP验证码函数代码(简单实用)
- php 简单实用 遍历文件夹代码
- 简单实用的php连接mysql封装类
- php 解决 引用 路径 php 中引用 ext 解决路径问题 简单实用
- PHP多文件上传(简单实用)
- php简单实用的操作文件工具类(创建、移动、复制、删除)
- 简单实用的网站PHP缓存类实例
- PHP_XmlRpc简单实现;
- 一个简单不过却很非常实用的PHP加密字符串方法
- PHP简单实用的数据分页显示代码
- php 一些实用简单栗子一 汉子转拼音
- 实用的简单PHP分页集合包括使用方法
- php生成xml的简单实用
- 实用的简单PHP分页集合包括使用方法
- 简单实用的分页函数(Php+mysql)
- PHP验证码函数代码(简单实用)
- 简单实用的PHP验证码类
- 简单实用的PHP防注入类实例
- 实用的简单PHP分页集合包括使用方法