基於php的json rpc原理及應用
json rpc 是一種以json為訊息格式的遠端呼叫服務,它是一套允許運行在不同作業系統、不同環境的程式實作基於Internet過程呼叫的規範和一系列的實作。這個遠端過程呼叫可以使用http作為傳輸協定,也可以使用其它傳輸協議,傳輸的內容是json訊息體。
下面我們code一套基於php的rpc框架,此框架中包含rpc的服務端server,和應用端client;
(一)(一)
。 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; } }
<?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; } } } ?>
<?php <span style="white-space:pre"> </span>require_once 'jsonRPCServer.php';
// member 为测试类 <span style="white-space:pre"> </span>require 'member.php'; <span style="white-space:pre"> </span>// 服务端调用 <span style="white-space:pre"> </span>$myExample = new member(); <span style="white-space:pre"> </span>// 注入实例 <span style="white-space:pre"> </span>jsonRPCServer::handle($myExample) or print 'no request'; ?>
(2)測試類別文件,member.php
class member{ public function getName(){ return 'hello word ' ; // 返回字符串 } }
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"; }

熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

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

熱門話題

許多用戶在選擇智慧型手錶的時候都會選擇的華為的品牌,其中華為GT3pro和GT4都是非常熱門的選擇,不少用戶都很好奇華為GT3pro和GT4有什麼區別,下面就給大家介紹一下二者。華為GT3pro和GT4有什麼差別一、外觀GT4:46mm和41mm,材質是玻璃鏡板+不鏽鋼機身+高分纖維後殼。 GT3pro:46.6mm和42.9mm,材質是藍寶石玻璃鏡+鈦金屬機身/陶瓷機身+陶瓷後殼二、健康GT4:採用最新的華為Truseen5.5+演算法,結果會更加的精準。 GT3pro:多了ECG心電圖和血管及安

Laravel是一款流行的PHPWeb開發框架,提供了許多有用的功能和元件,包括回應返回。在Laravel中,回應回傳是一個非常重要的概念,因為它控制著Web應用程式向客戶端提供資訊的方式。在本文中,我們將詳細介紹Laravel回應回傳的各種方式以及如何使用LaravelResponse回傳回應。返回字串Laravel中,可以使用Response物件的

request的中文意思是“請求”,是php中的一個全域變量,是一個包含了“$_POST”、“$_GET”和“$_COOKIE”的數組。 「$_REQUEST」變數可以取得POST或GET方式提交的資料、COOKIE資訊。

為什麼截圖工具在Windows11上不起作用了解問題的根本原因有助於找到正確的解決方案。以下是截圖工具可能無法正常工作的主要原因:對焦助手已開啟:這可以防止截圖工具開啟。應用程式損壞:如果截圖工具在啟動時崩潰,則可能已損壞。過時的圖形驅動程式:不相容的驅動程式可能會幹擾截圖工具。來自其他應用程式的干擾:其他正在運行的應用程式可能與截圖工具衝突。憑證已過期:升級過程中的錯誤可能會導致此issu簡單的解決方案這些適合大多數用戶,不需要任何特殊的技術知識。 1.更新視窗與Microsoft應用程式商店應用程

Python3.x中如何使用urllib.request.urlopen()函數發送GET請求在網路程式設計中,我們經常需要透過發送HTTP請求來取得遠端伺服器的資料。在Python中,我們可以使用urllib模組中的urllib.request.urlopen()函數來傳送HTTP請求,並取得伺服器回傳的回應。本文將介紹如何使用

第1部分:初始故障排除步驟檢查蘋果的系統狀態:在深入研究複雜的解決方案之前,讓我們先從基礎知識開始。問題可能不在於您的設備;蘋果的伺服器可能會關閉。造訪Apple的系統狀態頁面,查看AppStore是否正常運作。如果有問題,您所能做的就是等待Apple修復它。檢查您的網路連接:確保您擁有穩定的網路連接,因為「無法連接到AppStore」問題有時可歸因於連接不良。嘗試在Wi-Fi和行動數據之間切換或重置網路設定(「常規」>「重置」>「重置網路設定」>設定)。更新您的iOS版本:

PHP中的Request物件是用來處理客戶端傳送到伺服器的HTTP請求的物件。透過Request對象,我們可以取得客戶端的請求訊息,例如請求方法、請求頭資訊、請求參數等,從而實現對請求的處理和回應。在PHP中,可以使用$_REQUEST、$_GET、$_POST等全域變數來取得要求的信息,但是這些變數並不是對象,而是陣列。為了更靈活和方便地處理請求訊息,可

1.建立一個名為request.js的新文件,並匯入Axios:importaxiosfrom'axios';2、建立一個名為request的函數,並將其匯出:這將建立一個名為request的函數,並將其設定為具有基本URL的新的Axios實例。若要在封裝的Axios實例中新增逾時設置,可以在建立Axios實例時傳遞timeout選項。 exportconstrequest=axios.create({baseURL:'https://example.
