无为清净楼资源网 Design By www.qnjia.com

json rpc 是一种以json为消息格式的远程调用服务,它是一套允许运行在不同操作系统、不同环境的程序实现基于Internet过程调用的规范和一系列的实现。这种远程过程调用可以使用http作为传输协议,也可以使用其它传输协议,传输的内容是json消息体。

下面我们code一套基于php的rpc框架,此框架中包含rpc的服务端server,和应用端client;

(一)PHP服务端RPCserver jsonRPCServer.php

复制代码 代码如下:
class jsonRPCServer {
    /**
     *处理一个request类,这个类中绑定了一些请求参数
     * @param object $object
     * @return boolean
     */
    public static function handle($object) {
       // 判断是否是一个rpc json请求
        if ($_SERVER['REQUEST_METHOD'] != 'POST' || empty($_SERVER['CONTENT_TYPE'])
            ||$_SERVER['CONTENT_TYPE'] != 'application/json') {
            return false;
        }
        // reads the input data
        $request = json_decode(file_get_contents('php://input'),true);
        // 执行请求类中的接口
        try {
            if ($result = @call_user_func_array(array($object,$request['method']),$request['params'])) {
                $response = array ( 'id'=> $request['id'],'result'=> $result,'error'=> NULL );
            } else {
                $response = array ( 'id'=> $request['id'], 'result'=> NULL,
                                        'error' => 'unknown method or incorrect parameters' );}
        } catch (Exception $e) {
            $response = array ('id' => $request['id'],'result' => NULL, 'error' =>$e->getMessage());
        }
       // json 格式输出
        if (!empty($request['id'])) { // notifications don't want response
            header('content-type: text/javascript');
            echo json_encode($response);
        }
        return true;
    }
}

(二)Rpc客户端,jsonRPCClient.php
复制代码 代码如下:
<"\n".$request."\n".'***** End Of request *****'."\n\n";
        $opts = array ('http' => array (
                                    'method'  => 'POST',
                                    'header'  => 'Content-type: application/json',
                                    'content' => $request
        ));
        //  关键几部
        $context  = stream_context_create($opts);
  if ( $result = file_get_contents($this->url, false, $context)) {
            $response = json_decode($result,true);
  } else {
   throw new Exception('Unable to connect to '.$this->url);
  }
        // 输出调试信息
        if ($this->debug) {
            echo nl2br(($this->debug));
        }
        // 检验response信息
        if (!$this->notification) {
            // check
            if ($response['id'] != $currentId) {
                throw new Exception('Incorrect response id (request id: '.$currentId.', response id: '.$response['id'].')');
            }
            if (!is_null($response['error'])) {
                throw new Exception('Request error: '.$response['error']);
            }
            return $response['result'];

        } else {
            return true;
        }
    }
}
"codetitle">复制代码 代码如下:
<"codetitle">复制代码 代码如下:
// member 为测试类
require 'member.php';
// 服务端调用
$myExample = new member();
// 注入实例
jsonRPCServer::handle($myExample)
 or print 'no request';
"codetitle">复制代码 代码如下:
class member{
    public function getName(){
        return 'hello word ' ;  // 返回字符串
    }
}

(3)客户端 client.php
复制代码 代码如下:
require_once 'jsonRPCClient.php';

$url = 'http://localhost/rpc/server.php';
$myExample = new jsonRPCClient($url);

// 客户端调用
try {
 $name = $myExample->getName();
    echo $name ;
} catch (Exception $e) {
 echo nl2br($e->getMessage()).'<br />'."\n";
}

标签:
php,json,rpc框架

无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com

稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!

昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。

这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。

而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?