The application of PHP in app development

Source: Internet
Author: User
Tags error status code php write
PHP, as the development language of the server, plays the role of connecting client and database in app development, and the client implements the operation of the database by invoking the interface developed by PHP, which realizes the part of the user's business logic in PHP code. The client needs to pass some parameters to the server PHP, the format of these parameters by the client developers and service-side developers to negotiate the development, both follow the same set of standards, so that the data passed between the two can be correctly parsed. In the actual development of data in JSON format is widely used for client and server-side data interaction, almost every language supports the parsing of JSON data, in PHP using Json_encode () and Json_decode () can be very convenient.

The following points need to be noted in PHP for app development interfaces:

1, data transmission recommendations using Json,json has a strong cross-platform, most programming languages support JSON parsing, JSON is gradually replacing XML, become a common format for network data.

2, in order to ensure the security of the interface, must join the authentication system, to ensure that the request PHP interface is a legitimate source. In addition to the transmission of data can also use encryption technology, the 20th chapter of the book on the API interface signature and information encryption content.

3, for the online API, try to use error_reporting (0) to turn off the error prompt, or to write the error prompt to the log, easy to troubleshoot later. The purpose of this, on the one hand, can protect the interface security, prevent the output should not print error message, on the other hand is to ensure that the output is the correct data format, to prevent output error message by the client error parsing and the interface call exception occurs.

4, the development of the API and the WEB has a certain difference, if the interface returned by the format is not standardized, the client to get the resolution, may lead to client flash crashes and other situations, so the interface must be fully tested before the launch.

5, as far as possible to ensure that PHP write code performance, mobile phone applications than the Web application response speed requirements are higher, because of the huge differences in the performance of the user's phone, mobile phone applications in the data from the service end to data reorganization page rendering, etc. will consume more time than the Web application.

JSON is selected between the client and the server as the data transfer format, after which the meanings of the fields in the JSON are agreed, typically at least three fields are defined in the JSON data, return status codes, return status descriptions, and data content. For example, a JSON data that defines the return user information is as follows:

{"Code": 0, "MSG": "Success", "data": {"name": "Chenxiaolong", "Age": "$", "gender": "Male"}}

Where the code value of 0 indicates that the client interface is successful, the MSG field indicates the status of the request, and the return status code code corresponding to the data is the client wants to take the specific content, which contains the user information returned by the server. In the data field, developers can define different field formats based on the different interfaces they need.

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-& Gt;output ($data);} else {$this->output (', 1, ' invalid UID ');}}

By calling the GetUserInfo interface and passing in the UID parameters of the user, PHP receives the parameter to the MySQL database user table to query the user's information based on this UID, where user is a encapsulated user table model, It provides a findbyuid method of querying user information according to user UID, if the user information is queried to output the user information, otherwise the error message is returned to the client, the error status code returned here is defined as 1, which indicates an illegal UID, that is, there is no data record in the user table that corresponds to the UID.

The interface uses a common output method, which is a concrete implementation of the output JSON data, as shown in the following example code:

function output (, $data = ', $code =0, $msg = ' success ') {$out = array (' Code ' = $code, ' msg ' = $msg, ' data ' = * $data); echo Json_encode ($out);}

Note the echo output is used instead of return when the data is returned to the client.

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.