1. The interface can be divided into two types according to the requester:
1. It is an interface called by other internal projects (including js asynchronous Requested interfaces and timers).
2. It is an external interface, mainly provided for calls by external developers.
The biggest difference between the two interfaces is that the internal interface does not require strict authentication, while the external interface requires strict authentication. There are various encryption and decryption methods, the most common and simple of which is http basic authentication. , for example, the account and password pop-up window that pops up in our backend uses basic verification. You must enter your account and password to pass the verification. However, if the internal interface involves important operations and you do not want to be maliciously requested by others, you still need to do some encryption verification.
2. No matter which interface it is, the following issues should be taken into consideration:
1. Security. Parameter safety: For this we have $_INPUT which is basically enough. Whether authentication is required: If it is just an interface of the internal notification class/callback class, even if it is maliciously requested, the normal data will not be affected. In this case, there is no need to do verification at all. For example, I will make an interface to synchronize order status. The process inside is to query the orders with problematic order status, and then update the status of these orders. This kind of interface does not need to return any value, and even if it is maliciously called by a bad person, it will not be affected, so there is no need to perform authentication at all. Therefore, whether to add verification or not is determined according to the actual logic of the interface.
2. Can you repeat the request? For example, if an interface inserts a piece of data based on the incoming order number, it needs to make judgments about repeated requests to avoid inserting multiple pieces of data.
3. Write interface specifications
1. Use try...catch... structure to write. (Mainly throw can end the program at any time)
2. Return the status code. Do not return success equal to true or false. If you want to write a document, the meaning of different error codes should also be written clearly to facilitate the caller to query for errors. reason.
Example:
<?php $code = 200; // 接口状态码 $name = trim($_INPUT['name']); $age = trim($_INPUT['age']); if (empty($name)) { $code = 401; throw new Exception('名字不能为空'); } if (!is_numeric($age)) { $code = 402; throw new Exception('年龄必须由数字组成'); } $database_obj = new database_class(); $res = $database_obj->save($name, $age); if ( !$res ) { $code = 403; throw new Exception('保存数据失败'); } $msg = 'ok'; } catch ( Exception $e ) { $msg = $e->getMessage(); } output_json($code,$data,$msg); // $data可以放置需要返回的数据 // output_json函数在大function里面有~如果不想引入大function的话可以复制一份到自己项目 // ========END=======
Recommended tutorial: PHP video tutorial
The above is the detailed content of What are the types of php interfaces?. For more information, please follow other related articles on the PHP Chinese website!

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

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