基于php的json 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 <span style="white-space:pre"> 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 ' ; // 返回字符串 } }
(3)客户端 client.php
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"; }
以上就介绍了基于php的json rpc原理及应用,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

热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.
