[CODE:]XmlrpcClient.php
/** *******************************
* 2008-09-24 XMLRPC Client Class.
* Author : Mervin.G
* Call me : 76969318
* V 1.0
* *********************************/
class XmlrpcClient{
private $SendRequest; //发送的XML数据流
private $ReponseFiltrate = true; //XML数据过滤转换为php数据 开启
/** 设置发送的XML数据流 */
public function SetRequest($Request_Name='cycle', $Request_Value=array('egg','xgg','cgg')){
$this -> SendRequest = xmlrpc_encode_request($Request_Name, $Request_Value);
}
/** 发送并返回服务器结果
* $HostName - 服务器名称
* $HostPort - 服务器端口
* $URLpage - 接收数据的页面地址 */
public function SendRequest($HostName, $HostPort ,$URLpage){
$fp = fsockopen($HostName, $HostPort, $errno, $errstr); //打开远程页面
$query = "POST $URLpage HTTP/1.0\nUser_Agent: My Egg Client\nHost: ".$HostName."\nContent-Type: text/xml\nContent-Length: ".strlen($this -> SendRequest)."\n\n".$this -> SendRequest."\n"; //构造连接语句
if(!fputs($fp, $query, strlen($query))){ //打开并将构造连接语句写入
$errstr = "Write error";
return 0;
}
$contents = '';
while (!feof($fp)){ //如果不是文件最后一行
$contents .= fgets($fp); //将文件内容逐行读入
}
fclose($fp); //关闭远程连接
if($this -> ReponseFiltrate){
$split = ''; //设置分割字符串
$xml = explode($split, $contents); //按设置分割字符串为数组
$xml = $split.array_pop($xml); //将数组中有用数据取出
$xml = xmlrpc_decode($xml); //将XML数据流转换为PHP数据
return $xml;
}else{
return $contents;
}
}
}
?>
[CODE:]XmlrpcServer.php
/** *******************************
* 2008-09-24 XMLRPC Server Class.
* Author : Mervin.G
* Call me : 76969318
* V 1.0
* *********************************/
function lifecycle($method, $params){
$ClsName = $params[0]['clsname']; //远程调用类名
$FunName = $params[0]['funname']; //远程调用的函数名
$data = $params[0]['data']; //传进去处理的参数
$dateday = $params[0]['dateday']; //时间
$key = $params[0]['key']; //密码
require_once("../../Default.Setting.php"); //加载默认配置
$actionFile = SITE_PATH."/Class/Factory.php";
$hashcode = "123"; //密码设定
$localcheck = $dateday.$hashcode.$data;
if(md5($localcheck)==$key){
if(file_exists($actionFile)){ //检查要调用的文件是否存在
require_once($actionFile);
$actClass = Factory::factorynew('xmlrpc',$ClsName); //建立一个该类的对象
$tem_clsname = 'xmlrpc_'.$ClsName;
if($actClass instanceof $tem_clsname){ //检测$actClass是否已经是类$tem_clsname的对象
if (method_exists($actClass,$FunName)){ //检查该函数是否存在
return $actClass -> $FunName($data);
}else{
return 'Error : Function '.$FunName.' is not found!'; //未找到要调用的函数名
}
}else{
return $actClass; //未找到类文件
}
}else {
return 'Error : Factory.php is not found!'; //未找到要调用的类名
}
}else{
return 'Error : Password is wrong!'; //接收的数据或密码不正确
}
}
$output_options = array(
"output_type" => "xml",
"verbosity" => "pretty",
"escaping" => array("markup"), //这个设置是关键
"version" => "xmlrpc",
"encoding" => "utf-8"
);
$server = xmlrpc_server_create(); /** 建立XMLRPC操作对象 */
xmlrpc_server_register_method($server, "cycle.ck", "lifecycle"); /** 注册一个服务 */
$request = $HTTP_RAW_POST_DATA; /** 接收客户端发送过来的数据 */
$response = xmlrpc_server_call_method($server, $request, null, $output_options); /** 呼叫注册的方法 */
header('Content-Type: text/xml');
print $response;
xmlrpc_server_destroy($server); /** 销毁XML-RPC服务器端资源 */
?>
[CODE:]使用:客户端的使用
/** *********************************
* 2008-09-24 XMLRPC Client Example.
* Author : Mervin.G
* Call me : 76969318
* V 1.0
* ***********************************/
require_once("./Xmlrpc/XmlrpcClient.php"); /** 加载Xmlrpc客户端类 */
$HostName = 'localhost'; /** 服务器名称/地址 */
$HostPort = 80; /** 服务器端口号 */
$HostUrl = '/MVC-PTP/Services/Xmlrpc/XmlrpcServer.php'; /** XMLRPC服务的URL访问路径 */
$xml = new XmlrpcClient(); /** 建立XMLRPC对象 */
$classname = "egg"; /** 调用的类名 */
$functname = "xgg"; /** 调用的函数名 */
$hashcode = "123"; /** 密码 */
$dateday = "2008-09-25"; /** 日期 */
$data = "egg,xgg,egg"; /** 发送的数据 */
$key = md5($dateday.$hashcode.$data); /** 加密后的密码 */
$params = array('clsname' => $classname, 'funname' => $functname, 'dateday' => $dateday, 'data'=>$data, 'key'=>$key);
$xml -> SetRequest('cycle.ck', $params); /** 设置发送的数据和要调用的方法名 */
$response = $xml -> SendRequest($HostName,$HostPort,$HostUrl); /** 发送数据并返回 */
echo $response;
#print_r($response);
?>
[CODE:]使用:服务端的构建
require_once("./Xmlrpc/XmlrpcServer.php");
$xml = new XmlrpcServer("cycle", "lifecycle");
echo $xml -> ReturnXmlrpcResponse();
$xml -> XmlrpcServerClose();
function lifecycle($method, $params) {
switch($params[0]){
case 'egg': $reply = 'All eggs will be birds one day.'; break;
default: $reply = 'That must have been an otheregg';
}
return $reply;
}
?>
[CODE:]使用:调用的函数xmlrpc_egg.php
class xmlrpc_egg{
public function xgg($params) {
switch($params){
case 'egg,xgg,egg': $reply = '2222222222All eggs will be birds one day.'; break;
default: $reply = 'That must have been an otheregg';
}
return $reply;
}
}
?>
[CODE:]Default.Setting.php
define('SITE_PATH',str_replace('','/',dirname(__FILE__)));
$GLOBALS['ServicesPath'] = array(
"xmlrpc" => SITE_PATH."/Services/Xmlrpc/xmlrpcFunction/", //XMLRPC调用默认调用文件路径
"xmlrpc" => SITE_PATH."/Services/Xmlrpc/xmlrpcFunction/", //XMLRPC调用默认调用文件路径
);
?>
[CODE:]Factory.php
/** ******************************
* 2008-09-24 The factory method.
* Author : Mervin.G
* Call me : 76969318
* V 1.0
* ******************************* */
class Factory{
/** factory 加载对应服务下的类
* $type 对应的服务名称
* $classname 调用的类名 */
public static function factorynew($type,$classname){
if(file_exists($GLOBALS['ServicesPath'][$type] . $type.'_'.$classname . '.php')){
if(require_once($GLOBALS['ServicesPath'][$type] . $type.'_'.$classname . '.php')){
$renturnname = $type . '_' . $classname;
return new $renturnname;
}else{
throw new Exception ('Driver not found');
}
}else{
return $type.'_'.$classname.'.php is not found';
}
}
}
?>