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: XML-RPC 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.
客户端也可以用xmlrpc.inc
<?php
include("lib/xmlrpc.inc");
$GLOBALS['xmlrpc_internalencoding'] = 'UTF-8';
$client = new xmlrpc_client("/server.php", "localhost", 80);
$msg = new xmlrpcmsg('cycle',array(php_xmlrpc_encode("1")));
$res = $client->send($msg);
$val = $res->value();
echo $val->scalarval();;
?>
相关推荐
It is a library implementing the XML-RPC protocol, written in PHP. It is also known as PHPXMLRPC. It is designed for ease of use, flexibility and completeness. High speed and reduced memory footprint ...
php XmlRPC 官方文档 里面有详细的运用说明
PHP中集成了XML-RPC和SOAP两种协议的访问,都是集中在xmlrpc扩展当中。另外,在PHP的PEAR中,不管是PHP 4还是PHP 5,都已经默认集成了XML-RPC扩展,而且该扩展跟xmlrpc扩展无关,能够独立实现XML-RPC的协议交互,...
这里提供了一个开源的xmlrpc的包,可以实现php和c++的通信,提供多线程编程接口,省去了自己写socket的麻烦。 代码中包含一个实例:client端是php的,server端的c++的。
Zend_XmlRpc使用实例,zend 轻松实现webservice功能,对外接口
用于简单 PHP XMLRPC 客户端 下载 git clone https://github.com/tyd/supervisord-php.git 基本用法 require_once 'supervisord-php/lib/Supervisord.php'; $s = new Supervisord('127.0.0.1', 9001, 'username',...
xmlrpc php模块 和其他语言通讯很不错
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
xp下php5.2.17包 eAccelerator.dll php5.2.17_ext.zip php_bz2.dll ...php_tidy.dll php_xmlrpc.dll php_xsl.dll php_zip.dll xcache.dll xdebug.dll
PHP XML-RPC Server & Client 範例
离线安装包,亲测可用
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
快速介绍这是一个示例有效负载以及滥用 pfSense 的 xmlrpc.php 函数来建立后门并获得对 pfSense 防火墙的根级访问权限的示例。 这个漏洞是post-auth的(对于管理员账户),根据pfSense安全团队的说法,它被认为不是...
找不到php_mbstring.dll或php_exif.dll的解决方法
官方离线安装包,亲测可用。使用rpm -ivh [rpm完整包名] 进行安装
现在这个版本不好找,在此分享给大家。php-5.6.40-nts-Win32-VC11-x86.zip
【wordpress】 将class-wp-xmlrpc-server.php 上传至wordpress目录下的wp-includes目录下(/wp-includes/)替换原有xmlRpc 附言 | 应和[南博]版本号相同使用。不同版本有不同功能和算法。 若遇到问题,请到 反馈,...
php52-5.2.10-2.el5.i386.rpm php52-bcmath-5.2.10-2.el5.i386.rpm ...php52-xmlrpc-5.2.10-2.el5.i386.rpm php-oci8-11gR1-5.1.6-24.el5.i386.rpm php-oci8-11gR2-5.1.6-24.el5.i386.rpm
short_open_tag = On error_reporting = E_ALL & ~E_NOTICE ...extension=php_xmlrpc.dll extension=php_xsl.dll extension=php_zip.dll date.timezone = PRC session.save_path = "C:/php/temp"
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装