JSON_RPC_Client 客户端
/**
* @package JSON_RPC 客户端请求 * @author fengwei * */ class jsonRPCClient { /** * @desc Debug state * @var boolean */ static private $debug = false; /** * @desc 应用通信的key * @var string */ static private $key = "1.0_7413a20f00d1a1790d387822097ddae2"; /** * @desc 请求服务器的地址 * @var string */ static private $url = "http://localhost/api/index.php"; /** * @desc 应用的名称 * @var string */ static private $app = "tubo"; /** * @desc 接口 * @param string $api 请求的接口 * @param array $params 参数 */ static public function get($api, array $params){ if (empty($api) || !stripos($api, "/")) { throw new Exception(" 接口错误 : {$api} "); } $api = explode('/', $api); $times = time(); $request = array( 'model' => $api[0], 'action' => $api[1], 'params' => $params, 'keys' => array( 'app'=> self::$app, 'key'=> self::$key, 'times'=> $times, 'OAuthKey'=> md5( md5( self::$app ).self::$key.$times ), ), ); $request = json_encode($request); self::$debug && self::$debug.='***** Request *****'."\n".$request."\n".'***** End Of request *****'."\n\n"; //设置HTPP请求头, POST 请求,类型JSON $opts = array ( 'http' => array ( 'method' => 'POST', 'header' => 'Content-type: application/json', 'content' => $request, )); //创建请求的流 $context = stream_context_create($opts); //打开服务端链接 if ($fp = fopen(self::$url, 'r', false, $context)) { $response = ''; while($row = fgets($fp)) { $response.= trim($row)."\n"; } self::$debug && self::$debug.='***** Server response *****'."\n".$response.'***** End of server response *****'."\n"; echo ($response); $response = json_decode($response,true); } else { throw new Exception('服务端URL请求失败: '.self::$url); } // 打开调试信息 if (self::$debug) { echo nl2br($debug); } return $response; } }
//获取用户的信息 仿新浪微博接口
$rseult = jsonRPCClient::get("Member/getUserInfo", array("uid"=>'6'));
var_dump($rseult); ----------------------------------------------------------------------------------------------------------------------------------------------------------------
JSON_PRC_SERIVCE 服务器端 http://localhost/api/index.php
/**
* @desc JSON_PRC_Service * * 请求 参数格式说明 * * $param 必须为json 格式 * //验证信息 * $param["keys"] = array( * 'uid'=> * 'app'=> * 'times' * 'OAuthKey'=> * ) * $param["model"] // 请求的Model * $param["action"] // 请求的方法 * //请求方法所需的参数 * $param["param"] = array( * uid=> * username=> * ) */ class JSON_RPC_Service { /** * @desc 存放$_POST过来的数组 * @var unknown_type */ public $request = array(); /** * @desc 允许的App应用 * @var 数组的键值 必须为 定义常量的那个key; define('T_KEY', "xxxxx")如 T_KEY 是tubo的key */ public $apps = array("T_KEY"=>"tubo"); /** * @desc 存放 请求的对象 */ public $obj = ""; /** * @desc 初始化 */ public function __construct() { //保存POST过来的数据 $this -> request = json_decode(file_get_contents('php://input'),true);
//验证通讯的key
$this -> verifyKey(); //调用请求的方法
$this -> verifyMethod(); //输出数据
$this -> handle(); } /** *@desc 验证App应用是否合法 */ public function verifyKey() { if(!in_array($this->request['keys']['app'], $this->apps)){ $this->json_exit(array("state"=>false, "errorMsg"=>Error::getErrorByNo(Error::API_VERIFY_KEY_ERROR))); } //取得App的类型 ,如 tubo 是 T_KEY $this->request['keys']['key_type'] = $this->getKeyByValue($this->apps, $this->request['keys']['app']); $result = Tools::verifyKey($this->request['keys']); if(!$result){ $this->json_exit(array("state"=>false, "errorMsg"=>Error::getErrorByNo(Error::API_VERIFY_KEY_ERROR))); } return true; } /** *@desc 验证 请求的对象 和方法 */ public function verifyMethod() { if(!file_exists($filepath = LIB ."Openapi/{$this->request['model']}.class.php")){ $this->json_exit(array("state"=>false, "errorMsg"=>Error::getErrorByNo(Error::API_NOT_EXIEST_MODEL))); } $className = "Openapi_".$this->request['model']; $this->obj = new $className(); if(!@method_exists($this->obj, $this->request['action']) && $this->request['action']{0} != '_') { $this->json_exit(array("state"=>false, "errorMsg"=>Error::getErrorByNo(Error::API_NOT_EXIEST_METHOD))); } return true; } /** * @desc 取得结果 */ public function handle() { $data = call_user_func_array(array($this->obj, $this->request['action']),$this->request['params']); $this ->json_exit($data); } /** * @desc 输出结果 */ public function json_exit($data){ echo json_encode($data); exit(); } public function getKeyByValue($array, $value){ foreach($array as $k=>$v){ if ($value == $v){ return $k; } } } } //运行
new JSON_RPC_Service(); ---------------------------------------------------------------------------------------------------------
|