分享一下我写的JSON_RPC通讯机制,仿新浪微博的API
2012-07-21 13:24
531 查看
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(); --------------------------------------------------------------------------------------------------------- |
相关文章推荐
- SQLServer\framework启动报异常:Module的类型初始值设定项引发异常 [转]QQ空间、新浪微博、腾讯微博等一键分享API链接代码 window下 mongodb快速安装 数据表查询所有子/父节点 C#学习笔记1
- hadoop通讯(rpc)抓包解包工具(对分析hadoop内部机制或者问题定位很有帮助)
- 给Magento新增JSON-RPC类型的Api适配器(adapter)
- 给大家分享一下自己写的验证码api,有汉字、字母数字两类
- 利用ZABBIX的RPC-JSON作API扩展应用示例
- 简略整理一下RPC、WebService、Restful、Json、Xml这些概念之间的关系
- WEB开发中,使用JSON-RPC好,还是RESTful API好?
- 大数据架构中使用JSON-RPC好,还是RESTful API好?
- Bitcoin JSON RPC API - blockchain.info
- 终于找到了一篇能看懂的如何把javabean转换成json字符串的文章,嘿嘿,分享一下!
- QQ空间、新浪微博、腾讯微博等一键分享API链接代码
- WEB开发中,使用JSON-RPC好,还是RESTful API好?
- 以太坊JSON-RPC开源包分享
- 写了一个ES6(ES2015)模块可用的xml2json插件,给大家分享一下
- Zabbix 2.2 < 3.0.3 - RCE with API JSON-RPC
- 自制java一个json字符串生产,虽然有很多bug,还是分享一下
- 分享一下 Centos7 打开Docker 远程 API 的 配置
- WebService、RMI、RPC、XML-RPC、JSON-RPC、SOAP、REST(rest-api、restful)等web服务实现方案概念
- 晚上写程序的时候就是不知道STDAPI这些东西是搞什么的,分享一下!
- 新浪微博、腾讯微博、QQ空间、人人网、豆瓣 一键分享API