1. Review
I learned the instantiation object class of encapsulating MySQL!
2. This will encapsulate an app interface class for generating JSON data and XML data
3. Understanding and Mastering
3.1 The difference between XML and JSON
XML: Extensible Markup Language: Can tag data, define data type, data format is clear, readability is high;
JSON: A lightweight data interchange format; simple to generate data; fast transmission speed;
3.2 App interface and data
Get data: Get data from a database or cache (can be data in the cache)
Submit data: The data is submitted via get or post, and the data is returned after the server processing
3.3 Standard format for communication data
Status Code: Code
Hint Message: Message
Return Data:
3.4 Scheduled Tasks and caches (not encapsulated here)
Caching: Static caching, Memcache and Redis cache technology
Timed tasks: Corntab
4. Encapsulation
4.1 JSON encapsulation
Encapsulating interface Data methods in JSON mode
function Json_encode ();
Only UTF-8 encoded data is received
4.2 XML Encapsulation
Assemble string (Simple)
Methods of using the system
4.3 Implementation classes
$code, ' msg ' = $msg, ' data ' = $data), if ($type = = ' json ') {Self::jsonencode ($code, $msg, $data); exit ();} ElseIf ($type = = ' xml ') {Self::xmlencode ($code, $msg, $data); exit ();} ElseIf ($type = ' array ') {var_dump ($result); exit ();}} /** * 02. Output Communication Data as JSON * @param int $code Status code * @param string $msg hint * @param array $data data * Retrun string */public static function Jsonencode ($code, $msg = ", $data =array ()) {header (" Content-type:text/json "); #判断状态码if (!is_numeric ($ Code) {return ';} $result =array (' Code ' = $code, ' msg ' = $msg, ' data ' = $data); echo Json_encode ($result); exit ();} /** * 03. Encapsulate XML Output Communication data * @param unknown $code * @param unknown $msg * @param unknown $data */public STA Tic function Xmlencode ($code, $msg = ", $data =array ()) {if (!is_numeric ($code)) {return ';} $result =array (' Code ' = $code, ' msg ' = $msg, ' data ' = $data), header ("Content-type:text/xml"); $xml = "
"; $xml. ="
"; $xml. =self::xmltoencode ($result); $xml. ="
"; Echo $xml; exit ();} /** *04. Assemble the XML data * @param array $data * @return String * Use recursion to determine if the array is not the node that the array continues to call loop * XML cannot be a number, with item instead of */public static function Xmltoencode ($data) {$xml = $attr = "; foreach ($data as $key + = $value) {if (Is_numeric ($key)) {$attr =" id= ' {$key} ' "; $key = "Item";} $xml. = "<{$key} {$attr}>"; $xml. =is_array ($value)? Self::xmltoencode ($value): $value; $xml. = "
";} return $xml;}}? >
4.4 Calls
Through the URL implementation, display data types type can be XML, JSON, array!
Http://localhost:8081/appInterface/test.php?type=json
The test.php is implemented as follows:
Require_once ' apputil.php ';
$arr =array (' id ' =>1, ' name ' = ' yuan ', ' age ' =>23, ' location ' = ' hpu '); $arr 1=array (1,4,5,2,6,3); Response::jsonencode (+, ' success ', $arr);
5. Integration of the previous link MySQL database implementation: Data encapsulation
Call $con=db::getinstance ()->connect ();//Query Statement $sql= ' SELECT * from User_info ';//execute, return result set $result=mysql_query ($sql, $con);//Added new array $arr3=array (); while ($row =mysql_fetch_row ($result)) {Array_push ($arr 3, $row);} Response::show (' A ', ' success ', $arr 3);
6. apputil.php Download
http://download.csdn.net/detail/lablenet/8995987
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The above describes the Php-app interface implementation (JSON and XML), including aspects of the content, I hope the PHP tutorial interested in a friend to help.