《PHP應用:php實現(xiàn)的一個簡單json rpc框架實例》要點:
本文介紹了PHP應用:php實現(xiàn)的一個簡單json rpc框架實例,希望對您有用。如果有疑問,可以聯(lián)系我們。
json rpc 是一種以json為消息格式的遠程調用服務,它是一套允許運行在分歧操作系統(tǒng)、分歧環(huán)境的程序實現(xiàn)基于Internet過程調用的規(guī)范和一系列的實現(xiàn).這種遠程過程調用可以使用http作為傳輸協(xié)議,也可以使用其它傳輸協(xié)議,傳輸?shù)膬热菔莏son消息體.PHP實戰(zhàn)
下面我們code一套基于php的rpc框架,此框架中包括rpc的服務端server,和應用端client;PHP實戰(zhàn)
(一)PHP服務端RPCserver jsonRPCServer.phpPHP實戰(zhàn)
(二)Rpc客戶端,jsonRPCClient.php
PHP實戰(zhàn)
??? 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;
??? }PHP實戰(zhàn)
??? /**
???? *
???? * @param boolean $notification
???? */
??? public function setRPCNotification($notification) {
??????? empty($notification) ? $this->notification = false? : $this->notification = true;
??? }PHP實戰(zhàn)
??? /**
???? * @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');
??????? }PHP實戰(zhàn)
??????? if ($this->notification) {
??????????? $currentId = NULL;
??????? } else {
??????????? $currentId = $this->id;
??????? }PHP實戰(zhàn)
?????? // 拼裝成一個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'];PHP實戰(zhàn)
??????? } else {
??????????? return true;
??????? }
??? }
}
?>
PHP實戰(zhàn)
(三) 應用實例
(1)服務端 server.php
PHP實戰(zhàn)
(2)測試類文件,member.php
PHP實戰(zhàn)
(3)客戶端 client.php
PHP實戰(zhàn)
$url = 'http://localhost/rpc/server.php';
$myExample = new jsonRPCClient($url);PHP實戰(zhàn)
// 客戶端挪用
try {
?$name = $myExample->getName();
??? echo $name ;
} catch (Exception $e) {
?echo nl2br($e->getMessage()).'<br />'."\n";
}
PHP實戰(zhàn)
《PHP應用:php實現(xiàn)的一個簡單json rpc框架實例》是否對您有啟發(fā),歡迎查看更多與《PHP應用:php實現(xiàn)的一個簡單json rpc框架實例》相關教程,學精學透。維易PHP學院為您提供精彩教程。
轉載請注明本頁網(wǎng)址:
http://www.snjht.com/jiaocheng/11185.html