麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 擴展 > JSON > 正文

php實現的一個簡單json rpc框架實例

2024-09-06 20:18:14
字體:
來源:轉載
供稿:網友

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

  

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

復制代碼 代碼如下:
<?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

代碼如下:
<?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);

共2頁上一頁12下一頁
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 亚洲自拍第二页 | 久久精品99久久久久久2456 | 亚洲午夜电影 | 精品亚洲二区 | 91久久精品一 | www.777含羞草 | 国产精品一区二区三区在线 | 午夜视频免费播放 | 俄罗斯16一20sex牲色另类 | 国产 日韩 一区 | 国产午夜三级一区二区三桃花影视 | 午夜精品福利影院 | 欧美一级黄色免费看 | 天天鲁在线视频免费观看 | 看免费5xxaaa毛片 | 亚洲小视频在线 | 欧美亚洲黄色片 | 国产精品99久久99久久久二 | 中国杭州少妇xxxx做受 | 涩涩99| 极品国产91在线网站 | 精品久久久久久中文字幕 | 久久久久久91 | 国产第一页精品 | 久久久线视频 | 欧美综合在线观看 | 日本一级黄色毛片 | 久草干| qyl在线视频精品免费观看 | 可以看逼的视频 | 亚洲福利在线视频 | 日韩精品无码一区二区三区 | 国产免费传媒av片在线 | 国产免费一区二区三区视频 | 亚洲一区二区在线视频 | 免费看日产一区二区三区 | 狠狠撸电影 | 日本最新免费二区三区 | 国产精品国产成人国产三级 | 中国的免费的视频 | 少妇的肉体的满足毛片 |