PHP is returned to the app in JSON or XML format. This is a good thing to know, to wrap the data in JSON or XML and back to the app
To define the abstract app base class:
<?php/** * Definition API abstract class */abstract class API {Const JSON = ' JSON '; const XML = ' XML '; const ARR = ' Array ';/*** define factory method * param s Tring $type return data type */public static function factory ($type = Self::json) {$type = isset ($_get[' format ')? $_get[' format '): $type; $resultClass = Ucwords ($type); require_once ('./response/'. $type. '. php '); return new $resultClass ();} Abstract function response ($code, $message, $data);}
Return to the app in XML form:
<?phpclass Xml extends Api {public Function response ($code, $message = ", $data = Array ()) {if (!is_numeric ($code)) {RE Turn ';} $result = Array (' Code ' = $code, ' message ' = = $message, ' data ' = $data); header (' Content-type:text/xml '); $xml = " <?xml version= ' 1.0 ' encoding= ' UTF-8 '? >\n "; $xml. =" <root> "; $xml. = Self::xmltoencode ($result); $xml. =" </root> "; Echo $xml;} public static function Xmltoencode ($result) {$xml = $attr = "; foreach ($result as $key + = $value) { //Determine key value pairs, if yes The numeric key value does not allow if (Is_numeric ($key)) {$attr = "id=". $key. "'"; $key = "Item";} $xml. = "<{$key} {$attr}>"; Returned in a recursive form, mainly because the array is displayed in the XML as an array and must be displayed with specific key-value pairs of $xml. = Is_array ($value)? Self::xmltoencode ($value): $value; $xml. = "</{$key}>\n";} return $xml;}}
Return data in JSON format:
<?php/** * Output communication data as XML */class Json extends Api {public Function response ($code, $message = ', $data = Array ()) {if (! Is_numeric ($code)) {return ';} $result = Array (' Code ' = $code, ' message ' = = $message, ' data ' = $data); echo Json_encode ($result); exit;}}
You can also assemble the return data in this way:
<?phpclass Response {Const JSON = "JSON";/*** output communication data in an integrated manner * @param integer $code Status code * @param string $message message * @para M array $data data * @param string $type data type * return string*/public static function show ($code, $message = ', $data = array (), $type = Self::json) {if (!is_numeric ($code)) {return ';} $type = isset ($_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 ') {//For debugging Code Var_dump ($result);} elseif ($type = = ' xml ') {Self::xmlencode ($code, $message, $data); exit;} else {//todo}}/*** output Communication data as JSON * @param integer $code status code * @par AM string $message hint * @param array $data data * return string*/public static function json ($code, $message = ', $data = a Rray ()) {if (!is_numeric ($code)) {return ';} $result = Array (' Code ' = $code, ' message ' = $message, ' data ' = $data); echo Json_encode ($result); exit;} /*** output Communication data as XML * @pAram Integer $code Status code * @param string $message message * @param array $data data * return string*/public static function xmlencod E ($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::xmltoencode ($result); $xml. = " </root> "; Echo $xml;} public static function Xmltoencode ($data) {$xml = $attr = ""; foreach ($data as $key + = $value) {if (Is_numeric ($key)) {$a TTR = "id= ' {$key} '"; $key = "Item";} $xml. = "<{$key} {$attr}>"; $xml. = Is_array ($value)? Self::xmltoencode ($value): $value; $xml. = "</{$key}>\n";} return $xml;}}
PHP Development App Interface (i)