吴贺华 发表于 2017-3-3 10:02:32

php xmlrpc

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();;
 
?>
页: [1]
查看完整版本: php xmlrpc