欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php實(shí)現(xiàn)的一個(gè)簡(jiǎn)單json rpc框架實(shí)例

 更新時(shí)間:2015年03月30日 08:52:36   投稿:junjie  
這篇文章主要介紹了php實(shí)現(xiàn)的一個(gè)簡(jiǎn)單json rpc框架實(shí)例,本文給出了RPC服務(wù)端和客戶端代碼以及應(yīng)用實(shí)例,需要的朋友可以參考下

json rpc 是一種以json為消息格式的遠(yuǎn)程調(diào)用服務(wù),它是一套允許運(yùn)行在不同操作系統(tǒng)、不同環(huán)境的程序?qū)崿F(xiàn)基于Internet過(guò)程調(diào)用的規(guī)范和一系列的實(shí)現(xiàn)。這種遠(yuǎn)程過(guò)程調(diào)用可以使用http作為傳輸協(xié)議,也可以使用其它傳輸協(xié)議,傳輸?shù)膬?nèi)容是json消息體。

下面我們code一套基于php的rpc框架,此框架中包含rpc的服務(wù)端server,和應(yīng)用端client;

(一)PHP服務(wù)端RPCserver jsonRPCServer.php

復(fù)制代碼 代碼如下:

class jsonRPCServer {
    /**
     *處理一個(gè)request類,這個(gè)類中綁定了一些請(qǐng)求參數(shù)
     * @param object $object
     * @return boolean
     */
    public static function handle($object) {
       // 判斷是否是一個(gè)rpc json請(qǐng)求
        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);
        // 執(zhí)行請(qǐng)求類中的接口
        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

復(fù)制代碼 代碼如下:

<?php
/*
 */
class jsonRPCClient {

    private $debug;
    private $url;
    // 請(qǐng)求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) {
        // 檢驗(yàn)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;
        }

       // 拼裝成一個(gè)request請(qǐng)求
        $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
        ));
        //  關(guān)鍵幾部
        $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);
  }
        // 輸出調(diào)試信息
        if ($this->debug) {
            echo nl2br(($this->debug));
        }
        // 檢驗(yàn)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;
        }
    }
}
?>

(三) 應(yīng)用實(shí)例
(1)服務(wù)端 server.php

復(fù)制代碼 代碼如下:

<?php
require_once 'jsonRPCServer.php';

復(fù)制代碼 代碼如下:

// member 為測(cè)試類
require 'member.php';
// 服務(wù)端調(diào)用
$myExample = new member();
// 注入實(shí)例
jsonRPCServer::handle($myExample)
 or print 'no request';
?>

(2)測(cè)試類文件,member.php

復(fù)制代碼 代碼如下:

class member{
    public function getName(){
        return 'hello word ' ;  // 返回字符串
    }
}

(3)客戶端 client.php

復(fù)制代碼 代碼如下:

require_once 'jsonRPCClient.php';

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

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

相關(guān)文章

  • PHP讀取Excel類文件

    PHP讀取Excel類文件

    本篇文章主要介紹了PHP讀取Excel類文件的相關(guān)知識(shí)。具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧
    2017-05-05
  • php+mysqli使用面向?qū)ο蠓绞礁聰?shù)據(jù)庫(kù)實(shí)例

    php+mysqli使用面向?qū)ο蠓绞礁聰?shù)據(jù)庫(kù)實(shí)例

    這篇文章主要介紹了php+mysqli使用面向?qū)ο蠓绞礁聰?shù)據(jù)庫(kù)的方法,實(shí)例分析了mysqli對(duì)象的創(chuàng)建、連接、更新及返回更新結(jié)果的技巧,需要的朋友可以參考下
    2015-01-01
  • php中的buffer緩沖區(qū)用法分析

    php中的buffer緩沖區(qū)用法分析

    這篇文章主要介紹了php中的buffer緩沖區(qū)用法,結(jié)合實(shí)例形式分析了buffer緩沖區(qū)的概念、原理及php使用緩沖區(qū)相關(guān)存儲(chǔ)、輸出等操作技巧,需要的朋友可以參考下
    2019-05-05
  • php中用foreach來(lái)操作數(shù)組的代碼

    php中用foreach來(lái)操作數(shù)組的代碼

    php中用foreach來(lái)操作數(shù)組的代碼,需要的朋友可以參考下。
    2011-07-07
  • php全角字符轉(zhuǎn)換為半角函數(shù)

    php全角字符轉(zhuǎn)換為半角函數(shù)

    這篇文章主要介紹了PHP全角半角轉(zhuǎn)換函數(shù),把目前能找到的所有全角都列出來(lái)了一個(gè)個(gè)替換吧,需要的朋友可以參考下
    2014-02-02
  • shell腳本作為保證PHP腳本不掛掉的守護(hù)進(jìn)程實(shí)例分享

    shell腳本作為保證PHP腳本不掛掉的守護(hù)進(jìn)程實(shí)例分享

    以下是對(duì)用shell腳本作為保證PHP腳本不掛掉的守護(hù)進(jìn)程實(shí)例進(jìn)行了分析介紹,需要的朋友可以參考下
    2013-07-07
  • 詳解php命令注入攻擊

    詳解php命令注入攻擊

    這篇文章主要介紹了php命令注入攻擊,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • PHP常用函數(shù)和常見(jiàn)疑難問(wèn)題解答

    PHP常用函數(shù)和常見(jiàn)疑難問(wèn)題解答

    雖然PHP在整體功能上不如Java強(qiáng)大,但相比PHP而言Java算是較重量級(jí)的,所以在小中型系統(tǒng)的開(kāi)發(fā)上,使用PHP的趨勢(shì)不可擋,就算是大型網(wǎng)站,比如淘寶也部分使用了PHP(Java后臺(tái)邏輯+PHP前臺(tái)展示),所以趕緊開(kāi)始學(xué)PHP啦
    2014-03-03
  • php實(shí)現(xiàn)表單多按鈕提交action的處理方法

    php實(shí)現(xiàn)表單多按鈕提交action的處理方法

    這篇文章主要介紹了php實(shí)現(xiàn)表單多按鈕提交action的處理方法,需要的朋友可以參考下
    2015-10-10
  • PHP實(shí)現(xiàn)創(chuàng)建微信自定義菜單的方法示例

    PHP實(shí)現(xiàn)創(chuàng)建微信自定義菜單的方法示例

    這篇文章主要介紹了PHP實(shí)現(xiàn)創(chuàng)建微信自定義菜單的方法,結(jié)合實(shí)例形式分析了php創(chuàng)建微信自定義菜單的原理、步驟與具體實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2017-07-07

最新評(píng)論