Php app development interface

Source: Internet
Author: User
The interface keyword is used to define the php app interface. for example:

Interface video (){

Public function getVideos ();

Public function getCount (); // This is a virtual method.

}

Interface implementation: [all methods in the interface must be implemented in the example]

Class movie implements video {

Public function getVideo (){

// Do something

}

Public function getCount (){

// Do something

}

}

Interface address ?? Interface Data returned ?? Parsing data ?? Client

APP (communication) interface:

1, interface address (http://app.com/api.php? Format = xml );

2. interface file (api. php, processing some business logic );

3. Interface Data

How the APP communicates:

Client App triggering? Send an http request (interface address)? Server ??? Back to client

The returned data is in xml or json format.

The address is encapsulated in the app and invisible to users. it is different from the general web.

XML: the extension markup language. nodes can be customized (but HTML labels cannot be customized). The format is uniform and can be used across platforms. it is suitable for communication and transmission.

Example:

Singwa

Singwa1

Beijing

XML is highly readable; JSON is highly readable in terms of data generation and transmission speed.

Interface function: obtain and submit data.

Encapsulate communication interface data methods

JSON encapsulation communication interface: json_encode () [must be in the form of a UTF-8]

Example:

$ Arr = {

'Id' => 1;

'Username => Tom'

}

Echo json_encode ($ arr );

Iconv ('utf-8', 'gbk', $ data) // used for encoding conversion. In this example, $ data is converted from UTF-8 to GBK

Standard communication data format

1. code: 200 // status code

2. message // prompt message

3. data // return data

JSON encapsulation data method example (response. php ):

Class Respomse {

Public static function json ($ code, $ message = '', $ data = array ()){

If (! Is_numeric ($ code )){

Return ''; // if the passed $ code is not a number, return null.

}

$ Result = array (

'Code' => $ code,

'Message' => $ message,

'Data' => $ data

);

Echo json_encode ($ result );

Exit;

}

}

Call this interface in other files:

Require_once ('./response. php ');

$ Arr = array (

'Id' => 1,

'Name' => 'fareise'

);

Response: json (200, 'data returned successfully', '$ arr'); // This will return a json data

Interface Data encapsulation in XML format

How PHP generates XML data

1. assemble the string

2. use the system class (DomDocument; XMLWriter; SimpleXML)

Example:

Class Response {

Public static function xml (){

Header ("Content-Type: text/xml"); // converts a Type to an xml Type

$ Xml =" \ N"

$ Xml. =" \ N ";

$ Xml. ="200\ N ";

$ Xml. =" 200 \ N ";

......

$ Xml. ="";

Echo $ xml;

}

}

Encapsulate communication interfaces in XML format

Class Response {

Public static function xmlEncode ($ code, $ message, $ data ){

If (! Is_numeric ($ code )){

Return '';

}

$ Result = array (

'Code' => $ code,

'Message' => $ message,

'Data' => $ data,

);

Header ("Content-Type: text/xml ");

$ Xml =" "

$ Xml. =" ";

$ Xml. = self: xmlToEncode ($ result) // parse the data using the defined method xm

$ Xml. ="";

}

Publlc static function amlToEncode ($ data) {// write each piece of data as a tag through such traversal

$ Xml = $ attr = "";

Foreach ($ data as $ key => $ value) {// if the key is a number, convert it Format

If (is_numeric ($ key )){

$ Attr = "id = '{$ key }'";

$ Key = "item ";

}

$ Xml. = "<{$ key} {$ attr}> ";

// If an array is used, each element in the array is output using tags recursively.

$ Xml. = is_array ($ value )? Self: xmlToEncode ($ value): $ value;

$ Xml. =" "

}

}

}

Call example:

$ Data = array (

'Id' => 1,

'Name' => fareise

'Type => array (4, 5, 6 )'

)

Response: xmlEncode (200, 'sucess ', $ data );

Integrated Communication encapsulation method (both supported)

Based on the previous two methods:

Const JSON = 'json' // set the default value

Public static function show ($ code, $ message = '', $ data = array (), $ type = self: JSON ){

If (! Is_numeric ($ code )){

Return '';

}

$ Type = $ _ GET ['format']? $ _ GET ['format']: self "JSON;

$ Result = array (

'Code' => $ code,

'Message' => $ message,

'Data' => $ data

);

If ($ type = 'json '){

Self: json ($ code, $ message, $ data );

Exit;

} Elseif ($ type = 'array '){

Var dump ($ result)

} Elseif ($ type = 'xml '){

Self: xmlEncode ($ code, $ message, $ data );

Exit;

} Else {

// TODO

}

}

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.