Home Backend Development PHP Tutorial A brief discussion on developing APP interface with PHP (based on TP3.2 framework) (personal opinion)

A brief discussion on developing APP interface with PHP (based on TP3.2 framework) (personal opinion)

Apr 13, 2018 am 11:29 AM
php develop

The content shared with you in this article is about a brief discussion of PHP (based on TP3.2 framework) development of APP interface (personal opinion). It has certain reference value. Friends in need can refer to it

PHP is very powerful and can be used for various things, including web development, small programs, shopping malls, and of course APPs.
Since the blogger is also preparing to complete an APP project, I will write down my experience so that I can reflect on it in the future, haha.
Because we are writing interfaces, safety comes first, and we can’t kill anyone, right? So we have to negotiate an interface encryption method with the front-end, and each interface needs it (this can be called token encryption, or sign encryption, depending on what you like to call it)
Let me talk about how I encrypt it. Yes, I suggest that the interfaces are all delivered using post, so the following parameters are all based on post delivery

#1. First, sort the passed parameters in the key dictionary and remove the token value (PHP provides A ksort function, the default is standard ASICC code sorting. There is a pitfall here, that is, the sorting of IOS is sometimes different from that of Android, but only in some cases)
2. Concatenate the sorted values ​​(PHP provides a http_build_query function)
3. Splice a custom key after the sorted string (this should be consistent with the front end), and then md5 encryption
4. Convert it to uppercase as token and use it as a parameter.
Let’s post the code

function makeToken($data){
    //$data就是$_POST传过来的参数
    unset($data['token']);    unset($data['auth_key']); //这个下面会说到
    ksort($data);    $string = http_build_query($data);    if(empty($data)){        $string = 'key=CT01aVVsCkSxYdxi55ml';
    } else {        $string = $string .'&key=CT01aVVsCkSxYdxi55ml';
    }    $string = md5($string);    $result = strtoupper($string);    return $result;
}
Copy after login
Copy after login
<?phpnamespace Api\Controller;use Think\Controller;/**
 * 公共控制器
 */class CommonController extends Controller {

    public function _initialize(){
        // // //验证token
        $token = I(&#39;token&#39;);        $sal = makeToken($_POST);        if($sal!=$token){            $result = ajaxR(404,&#39;认证失败&#39;);            $this->ajaxReturn($result);
        }

    }
}
Copy after login
Copy after login

The token generated by the front end is passed in as a parameter, and then compared with the token you generated. If it is wrong, the token verification fails and the interface fails. No longer accessible.
Some interfaces are exceptions. They may request data directly without parameters, so you only need to encrypt the custom key with md5, that is, encrypt the string key=CT01aVVsCkSxYdxi55ml. Of course, this string You can do it however you like, the main thing is to negotiate with the front end.
Before parameter sorting, that is, before http_build_query, you need to remove the token and auth_key passed by the front end (not to mention this first), and then participate in the sorting. This must also be negotiated with the front end.

Let’s talk about auth_key next. Everyone knows that session is used to remember the user login status of the web page, and the APP also needs to log in the user status. Here I use a self-encrypted string to remember the user's login status, called auth_key parameter.
You can define the generation rules of auth_key yourself. After registering and logging in on the APP, store this string in the corresponding user and return it to the front end. Every access after the front end will carry this auth_key parameter. And you can query the relevant information of this user through this parameter.
Of course, you can also set a time limit on this auth_key, for example, give it a 7-day period, call it in every method of the project, and see if it has expired. If it expires, it will return a login status to the front end. Invalid, log out.
In fact, it is not difficult to develop the interface of the APP. The main thing is to negotiate with the front end and it will be easy to do. Generally, what we return is in json format. It is essential to define the returned status code, information and data as follows

{
    "code": 200,
    "message": "获取信息成功",
    "data": {
        "lng": "113.743393",
        "lat": "23.015902",
    }}
Copy after login
Copy after login

.

PHP is very powerful and can be used for various things, including web development, small programs, shopping malls, and of course APPs.
Since the blogger is also preparing to complete an APP project, I will write down my experience so that I can reflect on it in the future, haha.
Because we are writing an interface, safety comes first, and we can’t kill anyone, right? So we have to negotiate an interface encryption method with the front-end, and each interface needs it (this can be called token encryption, or sign encryption, depending on what you like to call it)
Let me talk about how I encrypt it. Yes, I suggest that the interfaces are all delivered using post, so the following parameters are all based on post delivery

#1. First, sort the passed parameters in the key dictionary and remove the token value (PHP provides A ksort function, which defaults to standard ASICC code sorting. There is a pitfall here, that is, the sorting of IOS is sometimes different from that of Android, but only in some cases)
2. Concatenate the sorted values ​​(PHP provides a http_build_query function)
3. Splice a custom key after the sorted string (this should be consistent with the front end), and then md5 encryption
4. Convert it to uppercase as token and use it as a parameter.
Let’s post the code

function makeToken($data){
    //$data就是$_POST传过来的参数
    unset($data[&#39;token&#39;]);    unset($data[&#39;auth_key&#39;]); //这个下面会说到
    ksort($data);    $string = http_build_query($data);    if(empty($data)){        $string = &#39;key=CT01aVVsCkSxYdxi55ml&#39;;
    } else {        $string = $string .&#39;&key=CT01aVVsCkSxYdxi55ml&#39;;
    }    $string = md5($string);    $result = strtoupper($string);    return $result;
}
Copy after login
Copy after login
<?phpnamespace Api\Controller;use Think\Controller;/**
 * 公共控制器
 */class CommonController extends Controller {

    public function _initialize(){
        // // //验证token
        $token = I(&#39;token&#39;);        $sal = makeToken($_POST);        if($sal!=$token){            $result = ajaxR(404,&#39;认证失败&#39;);            $this->ajaxReturn($result);
        }

    }
}
Copy after login
Copy after login

The token generated by the front end is passed in as a parameter, and then compared with the token you generated. If it is wrong, the token verification fails and the interface fails. No longer accessible.
Some interfaces are exceptions. They may request data directly without parameters, so you only need to encrypt the custom key with md5, that is, encrypt the string key=CT01aVVsCkSxYdxi55ml. Of course, this string You can do it however you like, the main thing is to negotiate with the front end.
Before parameter sorting, that is, before http_build_query, you need to remove the token and auth_key passed by the front end (not to mention this first), and then participate in the sorting. This must also be negotiated with the front end.

接下来说下auth_key吧,大家都知道session是用来记住web页面的用户登录状态的,而APP也是需要登录用户状态的。这里我使用的一个自己加密的一串用来记住用户登录状态,叫auth_key的参数。
auth_key的生成规则你可以自己定义,在APP端注册登录之后,把这个串存入相应的用户里面,并且将其返回给前端,前端之后的每个访问都带上这个auth_key这个参数,而你就可以通过这个参数来查询这个用户的相关信息。
当然,你也可以对这个auth_key进行一个时间的限制,例如给个7天的期限,在项目的每个方法都调用一下,看看是否过期了,过期了就给前端返回一个登陆状态失效,退出登录。
其实开发APP的接口不难,主要和前端协商好,就很容易办。一般我们返回的都是json格式,如下

{
    "code": 200,
    "message": "获取信息成功",
    "data": {
        "lng": "113.743393",
        "lat": "23.015902",
    }}
Copy after login
Copy after login

定义好返回的状态码和信息还有数据,这是必不可少的。

相关推荐:

浅谈PHP的跨域问题

浅谈PHP面向对象编程

浅谈php字符串反转实例详解

The above is the detailed content of A brief discussion on developing APP interface with PHP (based on TP3.2 framework) (personal opinion). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

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

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1669
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

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 vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

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 in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

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.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

See all articles