search
HomeBackend DevelopmentPHP Tutorial浅析Thinkphp框架中运用phprpc扩展模式

浅析Thinkphp框架中应用phprpc扩展模式

这次的项目舍弃了原来使用Axis2做web服务端的方案,改用phprpc实现,其一是服务端的thinkphp已集成有该模式接口,其二是phprpc传输的数据流相对于普通WebService中的XML或JSON形式的数据量明显减少,而且因为数据量的关系解析速度明显比较快~~

?

说实话,以前还真不知道有phprpc这个协议的,本打算使用sina的api的restlet形式开发,但自己写库的话会花比较多的时间,而现在轻量级的php框架支持rest的貌似只有cakephp,对于已经用tp部署的项目,实在是不情愿,这次偶尔在tp框架官网上徘徊一番~~忽然发现2.1RC版本有个新模式——phprpc。

?

用了才知道,phprpc这东西真的不错~~赞一个

?

但貌似这东西文档和案例比较的匮乏,而且tp框架也没有相应的教程和案例来使用这个模式(官方论坛里貌似也没有具体的使用方法http://bbs.thinkphp.cn/search.php?searchid=156&orderby=lastpost&ascdesc=desc&searchsubmit=yes)~~下面是我探索后写下的一些东西~~

?

首先认识下phprpc协议,具体文档(http://www.phprpc.org/zh_CN/docs/);

进入正题:

?

一、安装phprpc模式

?

?

1、将phprpc模式的文件夹(在官方SDK下的AddOns中,有一个phprpc文件夹和phprpc.php文件)拷到think核心文件下的mode文件夹中。


2、将phprpc的php的SDK拷到think核心文件下的Vendor文件夹中(要重命名为phprpc)


3、在入口文件添加如下代码:

?

define('THINK_MODE','PHPRPC'); 

? 4、在配置文件中添加如下配置代码:

?

'APP_PHPPRC_ACTIONS'=>'Account,Test,Topic,Timeline,User,Favorites,Follow'

?这里的字符串是要发布为服务的Action,可以多个,用逗号隔开

?

二、编写Action

?

首先来看看这边的phprpc模式到底tp做了什么。以下代码摘自phprpc自带的app.class.php

?

?

        Vendor('phpRPC.phprpc_server');    	//实例化phprpc    	$server = new PHPRPC_Server();        $actions =  explode(',',C('APP_PHPPRC_ACTIONS'));        foreach ($actions as $action){       	    //$server -> setClass($action.'Action'); 			$temp = $action.'Action';			$methods = get_class_methods($temp);			$server->add($methods,new $temp);	}	$server->setDebugMode(true);  	$server->setEnableGZIP(true);	$server->start();

?

? 这里导入了vendor里的phprpc核心类,再对每个在配置文件里要求发布的Action进行遍历,使所有的public方法全部发布。

?

在这里(http://bbs.thinkphp.cn/viewthread.php?tid=21593&highlight=phprpc)论坛中提到了在Action中start一个服务端,这其实是行不通的。上面的就是很好的解释,所以在所有的Action 的方法中只要有参数传入和参数return便可以以phprpc协议发布。

实例:

?

class TestAction extends Action{		/**	 * 	 * 测试欢迎	 * @param string $name	 */	function hello($name) {      	return 'Hello ' . $name;  	} }

?

?

三、关于Model not find的问题

?

假使在上述Action中调用M()工厂方法,会出现Model找不到的现象,这个问题搞了我很久,后来被我找到了解决方案在mode文件夹下的phprpc.php文件中加入:

?

THINK_PATH.'/Lib/Think/Core/Model.class.php', // 模型类

?这一配置项,对于视图模型找不到的现象的方法也是如此,下面是修改版的phprpc.php文件

?

return array(    THINK_PATH.'/Common/functions.php',   // 系统函数库    THINK_PATH.'/Lib/Think/Core/Think.class.php',    THINK_PATH.'/Lib/Think/Exception/ThinkException.class.php',// 异常处理    THINK_PATH.'/Lib/Think/Core/Log.class.php',// 日志处理    THINK_PATH.'/Mode/Phprpc/App.class.php', // 应用程序类    THINK_PATH.'/Mode/Phprpc/Action.class.php',// 控制器类    THINK_PATH.'/Lib/Think/Core/Model.class.php', // 模型类    THINK_PATH.'/Lib/Think/Core/Model/ViewModel.class.php', // 视图模型类    THINK_PATH.'/Mode/Phprpc/alias.php', // 加载别名);

?

四、关于Action方法返回问题

?

返回参数统一都是用return;

返回字符串可以用echo;

返回异常可以直接抛出异常throw new Exception('string', 1);

?

?

基本上载tp框架中使用phprpc模式的步骤就是这么几步~~大概等tp的后续版本应该会提供这方面的文档以供学习,也不用我们一点点的摸索了,不过最希望的还是tp能出个restlet模式~~哈哈

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

How does PHP handle object cloning (clone keyword) and the __clone magic method?How does PHP handle object cloning (clone keyword) and the __clone magic method?Apr 17, 2025 am 12:24 AM

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP vs. Python: Use Cases and ApplicationsPHP vs. Python: Use Cases and ApplicationsApr 17, 2025 am 12:23 AM

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

Describe different HTTP caching headers (e.g., Cache-Control, ETag, Last-Modified).Describe different HTTP caching headers (e.g., Cache-Control, ETag, Last-Modified).Apr 17, 2025 am 12:22 AM

Key players in HTTP cache headers include Cache-Control, ETag, and Last-Modified. 1.Cache-Control is used to control caching policies. Example: Cache-Control:max-age=3600,public. 2. ETag verifies resource changes through unique identifiers, example: ETag: "686897696a7c876b7e". 3.Last-Modified indicates the resource's last modification time, example: Last-Modified:Wed,21Oct201507:28:00GMT.

Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1?Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1?Apr 17, 2025 am 12:06 AM

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP: An Introduction to the Server-Side Scripting LanguagePHP: An Introduction to the Server-Side Scripting LanguageApr 16, 2025 am 12:18 AM

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

PHP and the Web: Exploring its Long-Term ImpactPHP and the Web: Exploring its Long-Term ImpactApr 16, 2025 am 12:17 AM

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

Why Use PHP? Advantages and Benefits ExplainedWhy Use PHP? Advantages and Benefits ExplainedApr 16, 2025 am 12:16 AM

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft