Application of php in app development

Source: Internet
Author: User
Tags error status code
As the development language of the server, php plays the role of connecting the client and database in app development. the client completes database operations by calling interfaces developed by php, php code that implements the user's business logic. The client needs to pass some parameters to php on the server. the format of these parameters... as the development language of the server, php plays the role of connecting the client and database in app development. the client completes database operations by calling interfaces developed by php, php code that implements the user's business logic. The client needs to pass some parameters to php on the server. the format of these parameters is determined by the client developers and server developers through mutual negotiation. The two parameters follow the same set of standards, the data transmitted between the two parties can be correctly parsed. In actual development, json data is widely used for interaction between the client and the server. almost every language supports json data parsing. in php, json_encode () and json_decode () are used () convenient.

Pay attention to the following points in the php app development interface:

1. json is recommended for data transmission. json is highly cross-platform. most programming languages support json parsing. json is gradually replacing xml and becoming a common format for network data.

2. to ensure interface security, you must add the authentication system to ensure that the php interface requested is a valid source. In addition, encryption technology can be used for transmitted data. Chapter 1 of this book describes the api interface signature and information encryption content.

3. for online APIs, use error_reporting (0) to close the error prompt, or write the error prompt to the log for future troubleshooting. The purpose of this operation is to protect the interface security and prevent the output of error messages that should not be printed, and to ensure that the output is in the correct data format, prevents interface call exceptions caused by client error parsing.

4. there are some differences between API development and WEB. if the format returned by the interface is not standard and the interface is parsed by the client, the client may crash and crash, therefore, you must perform a full test before launching the interface.

5. ensure the performance of the code written in php as much as possible. mobile apps have higher response speed requirements than web applications because of the huge performance difference of users' mobile phones, after the mobile app obtains data from the server, it will consume more time than the web app to perform data restructuring page rendering.

Json is selected between the client and the server as the data transmission format, and then the meaning of each field in json needs to be agreed. generally, at least three fields are defined in json data as the return status code, respectively, return status description and data content. For example, a json data that defines the returned user information is as follows:

{"Code": 0, "msg": "success", "data": {"name": "chenxiaolong", "age": "22", "gender ": "male "}}

The value of code 0 indicates that the client has successfully requested the interface. the msg field indicates the status of the request, which corresponds to the return status code. the data field indicates the specific content that the client wants to obtain, it contains the user information returned by the server. The data field developer can define different field formats based on different interface requirements.

A simple code example for this interface is as follows:

function getUserInfo() {$uid = $_REQUEST[‘uid'];$user = new User();if($data = $user->findByUid($uid) != false) {$this->output($data);} else {$this->output('',1,'invalid uid');}}

The client calls the getUserInfo interface and passes in the uid parameter of the User. php receives this parameter and queries User information in the User table of the mysql database based on this uid. the User is an encapsulated User table model, it provides the findByUid method to query User information based on the user uid. if user information is queried, the user information is output; otherwise, the error message is returned to the client. the returned error status code is defined as 1, invalid uid, that is, no data records corresponding to the uid are found in the user table.

The interface uses a public output method, which is the specific implementation of the output json data. the sample code is as follows:

function output(,$data='',$code=0,$msg='success') {$out = array('code'=$code,'msg'=>$msg,'data'=>$data);echo json_encode($out);}

Note that the echo output rather than return is used to return data to the client.

The above is a detailed description of the application of php in app development. For more information, see other related articles in the first PHP community!

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.