


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; }
<?phpnamespace Api\Controller;use Think\Controller;/** * 公共控制器 */class CommonController extends Controller { public function _initialize(){ // // //验证token $token = I('token'); $sal = makeToken($_POST); if($sal!=$token){ $result = ajaxR(404,'认证失败'); $this->ajaxReturn($result); } } }
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", }}
.
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['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; }
<?phpnamespace Api\Controller;use Think\Controller;/** * 公共控制器 */class CommonController extends Controller { public function _initialize(){ // // //验证token $token = I('token'); $sal = makeToken($_POST); if($sal!=$token){ $result = ajaxR(404,'认证失败'); $this->ajaxReturn($result); } } }
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", }}
定义好返回的状态码和信息还有数据,这是必不可少的。
相关推荐:
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!

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。


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

SublimeText3 English version
Recommended: Win version, supports code prompts!

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

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

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.