search
HomeBackend DevelopmentPHP ProblemHow to request interface with token in php

How to request interface with token in php

PHP Token(Token)

Token means "token", which is a string of characters generated by the server. An identifier for the client to make the request.

In view of the above characteristics, communication between the mobile terminal and the server requires 2 keys, that is, 2 tokens. (Recommended learning: PHP video tutorial)

The first token is for the interface (api_token);

The second token is for the user (user_token);

Let’s talk about the first token (api_token) first

Its responsibility is to maintain the concealment and effectiveness of interface access and ensure that the interface can only be used by one’s own family. How?

The reference idea is as follows:

Generate a random string based on the common attributes owned by the server and the client. The client generates this string, and the server also generates it according to the same algorithm. A string used to verify the client's string.

The current interface is basically MVC mode, and the URL is basically restful style. The general format of the URL is as follows:

http://blog.snsgou.com/模块名/控制器名/方法名?参数名1=参数值1&参数名2=参数值2&参数名3=参数值3

The interface token generation rules are as follows:

api_token = md5 ('模块名' + '控制器名' + '方法名' + '2013-12-18' + '加密密钥') = 770fed4ca2aabd20ae9a5dd774711de2

1. '2013-12-18' is the time of the day

2. 'Encryption key' is private Encryption key. After the mobile phone needs to register an "Interface User" account on the server, the system will assign an account and password. The data table design reference is as follows:

Field Name Field Type Comment

client_id varchar(20) Client ID

client_secret varchar(20) Client (encryption) key

Server interface verification, PHP implementation process is as follows:

<?php 
// 1、获取 GET参数 值 
$module = $_GET[&#39;mod&#39;]; $controller = $_GET[&#39;ctl&#39;] 
$action = $_GET[&#39;act&#39;]; $client_id = $_GET[&#39;client_id&#39;]; 
$api_token = $_GET[&#39;api_token‘]; 
// 2、根据客户端传过来的 client_id ,查询数据库,获取对应的 client_secret 
$client_secret = getClientSecretById($client_id); 
// 3、服务端重新生成一份 api_token 
$api_token_server = md5($module . $controller . $action .  date(&#39;Y-m-d&#39;, time()) .  $client_secret); 
// 4、客户端传过来的 api_token 与服务端生成的 api_token 进行校对,如果不相等,则表示验证失败 
if ($api_token != $api_token_server) { 
    exit(&#39;access deny&#39;);  // 拒绝访问 
    } 
// 5、验证通过,返回数据给客户端  
?>

The above is the detailed content of How to request interface with token in php. For more information, please follow other related articles on the PHP Chinese website!

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

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 Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version