1. The communication interface has two kinds of data structure XML and JSON, their difference:
|
Xml |
Json |
| 1. Readability |
Strong |
Weak |
| 2. Generate Data speed |
Weak |
Strong |
| 3. Transmission speed |
Weak |
Strong |
2. The following are 3 methods of encapsulation api.php
<?php
/*
* Three ways of encapsulating the app (communication) interface
* 1.json Mode Package Communication interface
* 2.xml Mode Package Communication interface
* 3.json-xml Integrated Mode package Communication interface
*/
Class response{
Const JSON = ' JSON ';
/* JSON mode
* @param $code Status Code
* @param $message Prompt information
* @param $data data
*/
public static function JSON ($code, $message = ', $data =array ()) {
if (!is_numeric ($code)) {//Judge status code, return NULL when not a number
Return ";
}
$code, $message, $data assembled into a new array
$result = Array (
' Code ' => $code,
' Message ' => $message,
' Data ' => $data
);
echo Json_encode ($result);
Exit
}
/* XML method
* @param $code Status Code
* @param $message Prompt information
* @param $data data
*/
public static function XML ($code, $message = ', $data =array ()) {
if (!is_numeric ($code)) {
Return ";
}
$result = Array (
' Code ' => $code,
' Message ' => $message,
' Data ' => $data
);
Header ("Content-type:text/xml");
$xml = "<?xml version= ' 1.0 ' encoding= ' utf-8 '" >\n ";
$xml. = "<root>\n";
$xml. =self::xmlencode ($result);
$xml. = "</root>";
Echo $xml;
}
public static function Xmlencode ($data) {
$xml = $arr = ';
foreach ($data as $key => $value) {
if (Is_numeric ($key)) {
$arr = "id = ' $key '";
$key = "Item";
}
$xml. = "<{$key} {$arr}>\n";
$xml. =is_array ($value) Self::xmlencode ($value): $value;
$xml. = "</{$key}>\n";
}
return $xml;
}
/* Json-xml Integrated mode
* @param $code Status Code
* @param $message Prompt information
* @param $data data
* @param select JSON $type communication type. Xml.
*
*/
public static function Show ($code, $message = ', $data =array (), $type) {
if (!is_numeric ($code)) {
Return ";
}
$type = isset ($_get[' format ')? $_get[' format ']:self::json;//to determine whether the client has a "format" field, if so, get it; otherwise, give the default JSON method
Assemble three parameters into a new array
$result = Array (
' Code ' => $code,
' Meaasge ' => $message,
' Data ' => $data
);
if ($type = = ' json ') {
Self::json ($code, $message, $data);
Exit
}elseif ($type = = "Array") {
Var_dump ($result);//debug mode only, not for communication
}elseif ($type = = ' xml ') {
Self::xml ($code, $message, $data);
Exit
}else{
Deal with specific business logic
}
}
}Here is the Test interface testapi.php
<?php
Require_once './api.php ';
$data =array (
' ID ' =>1,
' Name ' => ' php json ',
' title ' => "encapsulates the communication interface via JSON",
An XML----> node cannot be a number when the array is an indexed array, otherwise an error is encountered
' Type ' =>array (4,5,6),
Test when you are in a more complex array
' Test ' =>array (2,6,32=>array (3, "testing")
);
$result =array (
' ID ' =>1,
' Name ' => ' PHP xml ',
' title ' => "encapsulates the communication interface through XML,"
An XML----> node cannot be a number when the array is an indexed array, otherwise an error is encountered
' Type ' =>array (4,5,6),
Test when you are in a more complex array
' Test ' =>array (2,6,32=>array (3, "testing")
);
Response::json (200, ' Access data success ', $data);
Response::xml (200, ' Access data success ', $result);
Response::show (200, ' Access data success ', $result, ');//$type This parameter can be determined by the client to choose which communication interface Way XML or JSON when not selected, default to JSON (processed in api.php)
?>
3. Summary: According to the need to choose the right way