Posts Tagged ‘PHPRPC’

kohana中使用PHPRPC

Friday, November 13th, 2009

kohana版本: 2.34
PHPRPC介绍: PHPRPC

以下是实现kohana 使用 PHPRPC的具体实例,已经应用在一具体的项目中。功能不算强大,算是抛砖引玉吧。

Library 中 Rpc.php

<?php defined('SYSPATH') OR die('No direct access allowed.');

class RPC_Core {

private static $instance;

private static $data;

private static $server;

private $phprpc;

public static function instance()
{
if (!isset(self::$instance))
{
$class = __CLASS__;
self::$instance = new $class;
}
return self::$instance;
}

public function __construct()
{
//self::$server = "http://172.16.7.5";
$this->phprpc = TRUE;
$this->phprpc = FALSE;
self::$server = "http://site.fj";
}

public function __call($name, $args){

$class = $name;

$method = $args[0];

$args = array_slice($args,1);

if($this->phprpc)
{
require_once(MODPATH.'phprpc/phprpc_client.php');
$client = new PHPRPC_Client(self::$server.'/server/data/'.$class.'/'.$method);
return $client->{$method}($args);
}else{
$class = 'As_'.$class;
$class = new $class;
return $class->{$method}($args);
}
}
}

服务器上Controllers中的 server.php

<?php defined('SYSPATH') OR die('No direct access allowed.');

class Server_Controller extends Controller {

public function data($class)
{
$method = $this->uri->segment(4);
require_once(MODPATH.'phprpc/phprpc_server.php');

$server = new PHPRPC_Server();
$class = 'As_'.$class;
$server->add($method , new $class);
$server->start();
}
}

服务器端事物处理文件 Library 中 As_user.php

<?php defined('SYSPATH') OR die('No direct access allowed.');
class As_user_Core {
public function hello($name)
{
return 'hello world '.kohana::debug($name);
}

public function test()
{
return '-m-';
}
}

客户端测试Controllers 代码

public function test()
{
echo 'rpc test ...................................<br/>';
$rpc = Rpc::instance();
echo $rpc->user('hello',$class='class',$name='1');
}