"Reprint" PHP How to write interface to the app side

Source: Internet
Author: User
Tags what interface

How to write a good interface
    1. Purpose: To clearly understand the purpose of the written interface
    2. Security: Good interface security, prevent interface data leakage, do the necessary parameter encryption measures
    3. On demand: Accept values and return values to be practical, do not accept and return unwanted data, return values return what should be communicated with the user at all times
    4. Normative: The parameter naming specification has meaning, let the user be at a glance; The return value should contain the call interface state information, the return data format best Use JSON
    5. LOG: Write interface call log, easy to find interface call error, realize monitoring and docking fast
interface security and normative reference
/** * User Login Interface * Request method: Post * Accept parameter: * @param $mobile Username--Reversible via UrlEncode (Base64_encode ()) encryption      * @param $pwd encrypted password-reversible, and define a cryptographic rule * return JSON * {"code": int, "message": String, "Data": Array} * NOTE: *     */     Public functionLogin () {$mobile=isset($_post[' Mobile ']) ?Base64_decode(UrlDecode(Escapequotes ($_post[' Mobile '])) : ‘‘; $pwd=isset($_post[' pwd ']) ? Escapequotes ($_post[' pwd ']) : ‘‘; $TOTP=isset($_post[' Ttoken ']) ? Escapequotes ($_post[' Ttoken ']) : ‘‘; $commonmodel= Self::getmodel (' Common '); Try {            //Check Parameters            $commonmodel->checkright ($mobile, ' Mobile '); $commonmodel->checkright ($pwd, ' pwd '); $commonmodel->checkright ($TOTP, ' Ttoken '); //Verify Login            $usersmodel= Self::getmodel (' users '); $res=$usersmodel->userlogin ($mobile,$pwd); } Catch(Exception $e) {            $res=Array(                ' Code ' = 10000, ' message ' and ' = '$e-getMessage ()); }        //Write interface Log        $des= "IP:". GetIP (). "; Request the user to log on to the interface; return:".$res[' Message ']; $commonmodel->logresult (' 10001 ',$res[' Code '],$des); EchoJson_encode ($res); }

The accepted parameters, such as user name and password, are encrypted, the password can define a more secure encryption rule, and the packet view request parameter will not expose the user data.

The calling interface writes the interface log, logs what interface is called, returns the status code, the interface returns details, etc...

return value Reference
{    code:0,    message: "Success",    data: {key1:value1, key2:value2, ...  }}
    • Code: Return status Code, 1 for success, not 1 for various errors, 10000 to indicate client pass errors
    • Message: Description information, success is "Success", error message when wrong
    • Data: Returned on success, type is object or array

Different errors need to define different return status codes, here's an example:

Status Code table

Return value Description
10000 execution exception, general error for client pass parameter
10001 user does not exist
10002 Password Error
10003 User Status Not activated
10004 Redis Save Value Error
10005 Token Error
100,062-dimensional Code sequence error
10007 Errors in ticket verification
1 verification successful, return required information

10000 can be defined as an error in the parameters passed in by the client, throwing an exception directly, distinguishing between client and server errors

This article reprinted to: http://blog.csdn.net/xwh670570759/article/details/52130585

"Reprint" PHP How to write interface to the app side

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.