目錄
php实现的一个简单json rpc框架实例,jsonrpc框架实例
首頁 後端開發 php教程 php实现的一个简单json rpc框架实例,jsonrpc框架实例_PHP教程

php实现的一个简单json rpc框架实例,jsonrpc框架实例_PHP教程

Jul 13, 2016 am 09:59 AM
json php php語言 rpc 框架

php实现的一个简单json rpc框架实例,jsonrpc框架实例

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
复制代码 代码如下:
/*
 */
class jsonRPCClient {

    private $debug;
    private $url;
    // 请求id
    private $id;
    private $notification = false;
    /**
     * @param $url
     * @param bool $debug
     */
    public function __construct($url,$debug = false) {
        // server URL
        $this->url = $url;
        // proxy
        empty($proxy) ? $this->proxy = '' : $this->proxy = $proxy;
        // debug state
        empty($debug) ? $this->debug = false : $this->debug = true;
        // message id
        $this->id = 1;
    }

    /**
     *
     * @param boolean $notification
     */
    public function setRPCNotification($notification) {
        empty($notification) ? $this->notification = false  : $this->notification = true;
    }

    /**
     * @param $method
     * @param $params
     * @return bool
     * @throws Exception
     */
    public function __call($method,$params) {
        // 检验request信息
        if (!is_scalar($method)) {
            throw new Exception('Method name has no scalar value');
        }
        if (is_array($params)) {
            $params = array_values($params);
        } else {
            throw new Exception('Params must be given as array');
        }

        if ($this->notification) {
            $currentId = NULL;
        } else {
            $currentId = $this->id;
        }

       // 拼装成一个request请求
        $request = array(  'method' => $method,  'params' => $params,'id' => $currentId);
        $request = json_encode($request);
        $this->debug && $this->debug.='***** Request *****'."\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;
        }
    }
}
?>

(三) 应用实例
(1)服务端 server.php
复制代码 代码如下:
require_once 'jsonRPCServer.php';

复制代码 代码如下:
// member 为测试类
require 'member.php';
// 服务端调用
$myExample = new member();
// 注入实例
jsonRPCServer::handle($myExample)
 or print 'no request';
?>

(2)测试类文件,member.php
复制代码 代码如下:
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()).'
'."\n";
}

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/976039.htmlTechArticlephp实现的一个简单json rpc框架实例,jsonrpc框架实例 json rpc 是一种以json为消息格式的远程调用服务,它是一套允许运行在不同操作系统、不...
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前 By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

適用於 Ubuntu 和 Debian 的 PHP 8.4 安裝和升級指南 適用於 Ubuntu 和 Debian 的 PHP 8.4 安裝和升級指南 Dec 24, 2024 pm 04:42 PM

適用於 Ubuntu 和 Debian 的 PHP 8.4 安裝和升級指南

CakePHP 專案配置 CakePHP 專案配置 Sep 10, 2024 pm 05:25 PM

CakePHP 專案配置

CakePHP 日期和時間 CakePHP 日期和時間 Sep 10, 2024 pm 05:27 PM

CakePHP 日期和時間

CakePHP 檔案上傳 CakePHP 檔案上傳 Sep 10, 2024 pm 05:27 PM

CakePHP 檔案上傳

CakePHP 路由 CakePHP 路由 Sep 10, 2024 pm 05:25 PM

CakePHP 路由

討論 CakePHP 討論 CakePHP Sep 10, 2024 pm 05:28 PM

討論 CakePHP

如何設定 Visual Studio Code (VS Code) 進行 PHP 開發 如何設定 Visual Studio Code (VS Code) 進行 PHP 開發 Dec 20, 2024 am 11:31 AM

如何設定 Visual Studio Code (VS Code) 進行 PHP 開發

CakePHP 快速指南 CakePHP 快速指南 Sep 10, 2024 pm 05:27 PM

CakePHP 快速指南

See all articles