茄子在线看片免费人成视频,午夜福利精品a在线观看,国产高清自产拍在线观看,久久综合久久狠狠综合

    <s id="ddbnn"></s>
  • <sub id="ddbnn"><ol id="ddbnn"></ol></sub>

  • <legend id="ddbnn"></legend><s id="ddbnn"></s>

    php實現(xiàn)的一個簡單json rpc框架實例
    來源:易賢網(wǎng) 閱讀:2229 次 日期:2015-04-03 10:19:21
    溫馨提示:易賢網(wǎng)小編為您整理了“php實現(xiàn)的一個簡單json rpc框架實例”,方便廣大網(wǎng)友查閱!

    這篇文章主要介紹了php實現(xiàn)的一個簡單json rpc框架實例,本文給出了RPC服務(wù)端和客戶端代碼以及應(yīng)用實例,需要的朋友可以參考下

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

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

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

    代碼如下:

    class jsonRPCServer {

    /**

    *處理一個request類,這個類中綁定了一些請求參數(shù)

    * @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);

    // 執(zhí)行請求類中的接口

    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;

    // 請求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 *****'."nn";

    $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));

    }

    // 檢驗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)用實例

    (1)服務(wù)端 server.php

    代碼如下:

    <?php

    require_once 'jsonRPCServer.php';

    代碼如下:

    // member 為測試類

    require 'member.php';

    // 服務(wù)端調(diào)用

    $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);

    // 客戶端調(diào)用

    try {

    $name = $myExample->getName();

    echo $name ;

    } catch (Exception $e) {

    echo nl2br($e->getMessage()).'<br />'."n";

    }

    更多信息請查看IT技術(shù)專欄

    更多信息請查看網(wǎng)絡(luò)編程
    易賢網(wǎng)手機網(wǎng)站地址:php實現(xiàn)的一個簡單json rpc框架實例
    由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

    2026上岸·考公考編培訓(xùn)報班

    • 報班類型
    • 姓名
    • 手機號
    • 驗證碼
    關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機站點 | 投訴建議
    工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
    云南網(wǎng)警備案專用圖標(biāo)
    聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
    咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
    云南網(wǎng)警報警專用圖標(biāo)