: This article mainly introduces php-app interface implementation (json and xml). If you are interested in the PHP Tutorial, please refer to it. 1. Review
I learned how to encapsulate the instantiated object class of mysql in the previous article!
2. this article encapsulates an app interface class to generate json data and xml data.
3. understanding and understanding
3.1 Differences between xml and json
Xml: extended markup language: it can mark data and define data types. the data format is clear and easy to read;
Json: a lightweight data exchange format; simple data generation; fast transmission speed;
3.2 app interfaces and data
Get Data: get data from the database or cache (it can be the data in the cache)
Data Submission: data is submitted in get or post mode. after processing by the server, data is returned.
3.3 standard communication data format
Status code: code
Message: message
Returned data: data
3.4 scheduled task and cache (not encapsulated here)
Cache: static cache, Memcache and Redis Cache technology
Scheduled task: corntab
4. encapsulation
4.1 json encapsulation
Interface Data encapsulation in json format
Function json_encode ();
Only UTF-8 encoded data is received.
4.2 xml encapsulation
Assemble strings (simple)
How to use the system
4.3 implementation class
$ 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 in json mode * @ param int $ code Status code * @ param string $ msg prompt information * @ param array $ data * retrun string */public static function jsonEncode ($ code, $ msg = '', $ data = array () {header (" Conten T-Type: text/json "); # determine the status code 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 static 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 xml data * @ param array $ data * @ return string * use recursion to determine whether it is an array or not. the node that continues to call the loop * xml cannot be a number, use 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 call
Through url implementation, the data type can be xml, json, array!
http://localhost:8081/appInterface/test.php?type=json
The test. php implementation is as follows:
require_once 'appUtil.php';
$arr=array('id'=>1,'name'=>'yuan','age'=>23,'location'=>'hpu');$arr1=array(1,4,5,2,6,3);Response::jsonEncode(200,'success',$arr);
5. integration with the mysql database in the previous article: Data encapsulation
// Call $ con = Db: getInstance ()-> connect (); // query statement $ SQL = 'select * from user_info '; // run, returned result set $ result = mysql_query ($ SQL, $ con); // New array added $ arr3 = array (); while ($ row = mysql_fetch_row ($ result )) {array_push ($ arr3, $ row);} Response: show ('20140901', 'success ', $ arr3 );
6. download appUtil. php
Http://download.csdn.net/detail/lablenet/8995987
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.
The above introduces the php-app interface implementation (json and xml), including some content, and hope to be helpful to friends who are interested in PHP tutorials.