PHP开发API的1点体会
PHP开发API的一点体会
本文章是个人在自我开发了一些APP接口后,以及阅读了一些资料后的体会。
一、功能模块设计
从需求分析中抽离出相应的功能,这部分直接关系到我们的程序员需要实现这个应用的什么功能。例如:注册&登陆。
二、应用架构设计
- 对于整个应用来说,我们的架构是C/S模式。客户端使用Android&IOS,服务端使用一种服务端开发语言来提供相应接口。然后客户端通过HTTP协议来获取或者发送相应的请求。
- 确定客户端与服务端使用何种形式的协议进行交付。通常情况下会使用json。
- 然后确定服务端采用何种架构。例如:底层使用mysql、中间层用户提供各种服务、最上层封装接口,提供客户端使用。以及提供管理后台(并不是所有的移动应用都需要管理后台)
三、通信协议的定义原则
1、通用性,前期设计尽可能把情况考虑全面一点,否则当开发一段时间后,再来修改协议,将会非常麻烦。或者为了将就,会导致协议中的数据解析麻烦、冗余不断增加;
2、简洁性,移动端与服务端是通过网络来传输的,协议越简洁,交付速度就越快。体验就越好;
3、统一编码,为了兼容性问题。一般目前客户端与服务端统一采用UTF-8编码方式。
个人采用的协议格式:
<code class=" hljs json">{ "<span class="hljs-attribute">code</span>":<span class="hljs-value"><span class="hljs-string">"正确或者错误的代码号"</span></span>, "<span class="hljs-attribute">message</span>":<span class="hljs-value"><span class="hljs-string">"对应代码号的提示信息"</span></span>, "<span class="hljs-attribute">data</span>":<span class="hljs-value"><span class="hljs-string">"返回的数据内容"</span></span>}</code>
在返回的数据区data可能会有三种情况:单个对象数据、返回多个对象同一种数据、返回单个对象及多个对个对象数据。
四、数据库设计
这个是必不可少的,无需多言。这里需要声明的是,必要的增加一些冗余字段,会大大减少查询时候的速度。所以在概要设计时,可以适当的考虑。
五、服务端程序架构设计(这里使用php做说明)
- 底层使用何种php框架,何种模板解析引擎
- 整个应用的library层
- 最上层是app mvc层(即API接口)
六、客户端程序架构设计
客户端由于是在Andorid或者IOS核心类库的基础上建立起来的,相当只需要定义上层架构,底层已经由相应的移动系统定义好。
不管是Android还是IOS或者其它移动系统。个人认为在这个架构中应该包括以下几个部分:
会使用到的工具类,如:图片操作(上传,浏览,删除)。文件操作等
Test相关类,主要是进行自测的一些测试代码
UI相关的类,MVC中的View层
Model类,MVC中的Model层
Service类,与服务相关的所有类
Sqlite类,存放SQLite数据库的操作类(并不是所有应用都需要)
七、提高性能
- 压力测试:使用apache ab工具。将其加入到环境变量。打开终端输入ab命令操作。
参数说明:-n 总请求数 -c 并发请求数 -p POST请求文件 -t POST请求头
例:ab –n 100 –c 10 http://api.com/index 通常使用10个并发完成100个请求来粗略估计接口性能。- 语言角度的优化
a、减少include和require:这个问题可以使用安装APC组件来完成
b、用局部变量代替全局变量。
c、尽量使用静态函数或方法:静态方法会提升执行速度
d、释放哪些不用的变量或者资源:不过分依赖PHP的内存回收机制。程序中不用的变量或者资源及时释放。可以使用unset或者直接设置为null。
e、用单引号代替双引号。
[email protected]。
g、正则表达式在PHP中运行效率不高,尽量少使用。
h、对存储的数据尽量压缩。
i、升级到新版PHP版本。 - 结构层次的优化
使用缓存中间件:Memcache和Redis, 主要是加快读取速度,降低数据库查询压力。 - 压缩数据
php中主要使用gzip进行压缩 - 其它优化手段
a、对服务器进行相关配置。迁移到linux服务器。
b、数据库优化,如:尽可能使用确定性查询语句,建立索引,主从数据结构(master/slave),集群等。
c、增大服务器带宽,选择良好的运营商。
- 语言角度的优化

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.
