What should I pay attention to when writing an api in php?

Source: Internet
Author: User
Tags mysql connect
The first time I wrote an api program, I used native php, and there was no restful requirement. If you only implement the function to return json data, it is easy to implement most of the functions. What should you pay attention to besides the function implementation? New users seek guidance .. The Code has been pasted into a total of 4 files {code...} {... the first time I wrote the api program, using native php, there is no restful requirement. If you only implement the function to return json data, it is easy to implement most of the functions. What should you pay attention to besides the function implementation? New users seek guidance .. Code has been attached to a total of 4 files


  $ Code, 'message' => $ message, 'data' => $ data); echo json_encode ($ result );}}

  '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_connect ($ 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 logon code.

/** Login. phh logon interface */
  Connect ();} catch (Exception $ ex) {return Response: show (402, "database connection failed");} $ 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, 'logon successfully');} else {return Response: show (202, 'incorrect password');} else {return Response: show (201, 'account does not exist ');}
/** GetPersonalInfo. phh interface for querying personal information */
  Connect ();} catch (Exception $ ex) {return Response: show (402, 'database connection failed ');} $ account = isset ($ _ GET ['account'])? Mysqli_real_escape_string ($ link, trim ($ _ GET ['account']): null; if ($ account = null) {return Response: show (401, 'Not logged on');} $ query = "select * from users where account = \" $ account \ ""; $ result = mysqli_query ($ link, $ query ); $ row = mysqli_fetch_assoc ($ result); return Response: show (200, "Operation successful", $ row );

Reply content:

The first time I wrote an api program, I used native php, and there was no restful requirement. If you only implement the function to return json data, it is easy to implement most of the functions. What should you pay attention to besides the function implementation? New users seek guidance .. Code has been attached to a total of 4 files


  $ Code, 'message' => $ message, 'data' => $ data); echo json_encode ($ result );}}

  '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_connect ($ 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 logon code.

/** Login. phh logon interface */
  Connect ();} catch (Exception $ ex) {return Response: show (402, "database connection failed");} $ 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, 'logon successfully');} else {return Response: show (202, 'incorrect password');} else {return Response: show (201, 'account does not exist ');}
/** GetPersonalInfo. phh interface for querying personal information */
  Connect ();} catch (Exception $ ex) {return Response: show (402, 'database connection failed ');} $ account = isset ($ _ GET ['account'])? Mysqli_real_escape_string ($ link, trim ($ _ GET ['account']): null; if ($ account = null) {return Response: show (401, 'Not logged on');} $ query = "select * from users where account = \" $ account \ ""; $ result = mysqli_query ($ link, $ query ); $ row = mysqli_fetch_assoc ($ result); return Response: show (200, "Operation successful", $ row );

Update

  • Complete data

  • Concise Semantics

  • Version controllable

  • Flexible Response

  • Secure and reliable (https can be considered, token mechanism, ip whitelist, etc)

  • Response Speed

  • Unified style (do not change every three differences, and the returned format is unified)

Remember, do not write json_ecode ($ array); exit.
Use your own lightweight package similar to apirespneuron ($ data), although you only return json.

I can add one more.
Unified format

For example, the returned data must contain a "Request status result" with a uniform name. For example, the returned data must be stored in a variable.
Avoid identifying different names in different places on the front-end.

The registration interface and the lottery interface must be secured to prevent malicious scams !!!

Paste your code first. I also want to learn.

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.