What do you need to be aware of in PHP writing API?

Source: Internet
Author: User
Tags mysql connect
First Write API program, using native PHP, no restful requirements. If you only implement functionality to return JSON data, it feels easy to implement most of the functionality, so what do you need to be aware of in addition to functional implementation? Novice seeking guidance. Already posted code altogether 4 files


  
    $code,            'message' => $message,            'data' => $data        );                echo json_encode($result);    }}
!--? php/* * Singleton mode database connection */require_once (' response.php '); class Db {static private $_instance; static private $_connectsource; Private $_dbconfig = Array (' host ' =--> ' localhost ', ' user ' = ' root ', ' password ' = ' xxx ', ' Database ' = ' sportsstore '); Private Function __construct () {} static public function Getinstace () {if (! ( Self::$_instance instanceof Self)} {self::$_instance = new self (); } return self::$_instance; Public Function Connect () {if (!self::$_connectsource) {Self::$_connectsource = Mysqli_c Onnect ($this->_dbconfig[' host '), $this->_dbconfig[' user ', $this->_dbconfig[' password '], $this ->_dbconfig[' database '); if (!self::$_connectsource) {throw new Exception ("MySQL connect error". Mysql_errno ()); }} return Self::$_connectsource; }}

This is the login code.

/**login.phh 登录接口*/
  
   connect();} catch (Exception $ex) {    return Response::show(402,"数据库连接失败");}$account = mysqli_real_escape_string($link, trim($_POST['account']));$password = mysqli_real_escape_string($link, trim($_POST['password']));$query = "select * from users where account = \"$account\"";$result = mysqli_query($link, $query);if(mysqli_num_rows($result)==1){    $row = mysqli_fetch_array($result);    if($row['password'] == $password){        return Response::show(200, '登录成功');    }else{        return Response::show(202,'密码不正确');    }}else{    return Response::show(201,'账户不存在');}
/**getPersonalInfo.phh 查询个人信息接口*/
  
   connect();} catch (Exception $ex) {    return Response::show(402,'数据库连接失败');}$account = isset($_GET['account']) ? mysqli_real_escape_string($link, trim($_GET['account'])) : null;if($account === null){    return Response::show(401,'未登录');}$query = "select * from users where account=\"$account\"";$result = mysqli_query($link, $query);$row = mysqli_fetch_assoc($result);return Response::show(200,"操作成功",$row);

Reply content:

First Write API program, using native PHP, no restful requirements. If you only implement functionality to return JSON data, it feels easy to implement most of the functionality, so what do you need to be aware of in addition to functional implementation? Novice seeking guidance. Already posted code altogether 4 files

 !--? phpclass Response {/** * Output the communication data in JSON * @param integer $cod E Status code * @param string $message message * @param array $data data */public static function show ($code, $message =        ", $data = Array ()) {if (!is_numeric ($code)) {return '; } $result = Array (' Code ' =--> $code, ' message ' = = $message, ' data ' =&G T                $DATA);    echo Json_encode ($result); }}
!--? php/* * Singleton mode database connection */require_once (' response.php '); class Db {static private $_instance; static private $_connectsource; Private $_dbconfig = Array (' host ' =--> ' localhost ', ' user ' = ' root ', ' password ' = ' xxx ', ' Database ' = ' sportsstore '); Private Function __construct () {} static public function Getinstace () {if (! ( Self::$_instance instanceof Self)} {self::$_instance = new self (); } return self::$_instance; Public Function Connect () {if (!self::$_connectsource) {Self::$_connectsource = Mysqli_c Onnect ($this->_dbconfig[' host '), $this->_dbconfig[' user ', $this->_dbconfig[' password '], $this ->_dbconfig[' database '); if (!self::$_connectsource) {throw new Exception ("MySQL connect error". Mysql_errno ()); }} return Self::$_connectsource; }}

This is the login code.

/**login.phh 登录接口*/
  
   connect();} catch (Exception $ex) {    return Response::show(402,"数据库连接失败");}$account = mysqli_real_escape_string($link, trim($_POST['account']));$password = mysqli_real_escape_string($link, trim($_POST['password']));$query = "select * from users where account = \"$account\"";$result = mysqli_query($link, $query);if(mysqli_num_rows($result)==1){    $row = mysqli_fetch_array($result);    if($row['password'] == $password){        return Response::show(200, '登录成功');    }else{        return Response::show(202,'密码不正确');    }}else{    return Response::show(201,'账户不存在');}
/**getPersonalInfo.phh 查询个人信息接口*/
  
   connect();} catch (Exception $ex) {    return Response::show(402,'数据库连接失败');}$account = isset($_GET['account']) ? mysqli_real_escape_string($link, trim($_GET['account'])) : null;if($account === null){    return Response::show(401,'未登录');}$query = "select * from users where account=\"$account\"";$result = mysqli_query($link, $query);$row = mysqli_fetch_assoc($result);return Response::show(200,"操作成功",$row);

Update

    • Data integrity

    • Semantic simplicity

    • Version controllable

    • Flexible response

    • Safe and reliable (https can be considered, token mechanism, IP whitelist, etc.)

    • Response speed

    • Uniform style (don't go out, return to the same format)

Remember, don't write Json_ecode ($array); exit; Such code is good.
With your own lightweight like apirespnse ($data), though you only return JSON.

The upstairs is good, I add one more.
Uniform format

For example, the returned data must have a uniform name of the "Request status results", such as the returned data must be uniformly placed in a variable.
Avoid the presence of different places in the foreground to judge different names.

Registered interface and draw the interface must be secure to prevent malicious brush!!!

You put your code on first. I'll learn a bit too.

  • Related Article

    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.