<span id="Label3"></p>The first part, the realization of communication interface<p><p>Tags (space delimited): PHP mobile Backend API Communication Interface Andy</p></p><p style=""><p style="">When PHP develops the phone api, it generally returns data of XML or JSON data type, in addition to returning the status code from the source data (the data required by the program itself), the following is a encapsulated data, which is presented in JSON format:<br></p></p><pre><pre>{ code:200, message: "Success", data: [ { username: "Amming", password: "123456", level : " 1 ", gold:" 0 ", id:" 6 ", create_time:" 2015-09-26 08:25:31 ", is_active:" 1 ", is_admin:" 0 " }, { username: "zhang san", password: "12345", level : "1", gold: "0", id: "8", Create_time: "0000-00-00 00:00:00", is_active: "1", is_admin: "0" } ]}</pre></pre><p><p>The following is an example of XML format Data:</p></p><pre><pre><root> <code>200</code> <message>Success</message> <data> <item id= "0" > <username> Amming </username> <password>123456</password> <level>1</level> <gold>0</gold> <id>6</id> <create_time> 2015-09-26 08:25:31</create_time> <is_active>1</is_active> <is_admin>0</is _admin> </item> <item id= "1" > <username> Zhang San </username> < password>12345</password> <level>1</level> <gold>0</gold> < id>8</id> <create_time>0000-00-00 00:00:00</create_time> <is_active>1</ is_active> <is_admin>0</is_admin> </item> </data></root></pre></pre><p><p>In addition, to facilitate the debugging of mobile phone developers, You can also directly return data with data type:</p></p><pre><pre>Array (3) { ["code"]=> int ($) ["message"]=> string (7) "Success" ["data"]=> Array (2) { [0]=> array (8) { ["username"]=> string (9) "Amming" ["password"]=> String (6) "123456" ["level"]=> string (1) "1" ["gold"]=> string (1) "0" ["id"]=> string (1) "6" ["create_time"]=> string () "2015-09-26 08:25:31" ["is_active"]=> string (1) "1" ["is_ Admin "]=> string (1)" 0 " } [1]=> array (8) { [" username "]=> string (6)" Zhang San " ["password"]=> string (5) "12345" ["level"]=> string (1) "1" ["gold"]=> String (1) "0" ["id"]=> string (1) "8" ["create_time"]=> string (19) "0000-00-00 00:00:00" ["is_active"]=> string (1) "1" ["is_admin"]=> string (1) "0" }}}</pre></pre>How to Achieve<p><p>When the mobile client gets the data through the api, the PHP script response an array and encode the array, each of which is json,xml and an array, defined as Follows:</p></p><pre><pre>$result = ["code" = "+", " message" = "data returned successfully", "value" = " key" = "=", "key" = "value"] ]</pre></pre><p><p>Where code represents the status code, the message represents the status information, and data is required in the program Logic.</p></p>How to design<p><p>When the mobile phone side calls the api, the program business logic processing is completed, the need to return data, the communication data needs to be encapsulated, the package of three types by the request can be specified by the format parameter, when Formart=json execution Response_json method, similarly, There are also Response_xml and Response_array methods;</p></p><p><p>To facilitate invocation, write a response class to encapsulate the data and complete the response work:</p></p><pre>/* This code is not tested by the actual environment, there is no rigorous parameter check */class response{public static function Response_api ($code, $message = ", $data =array ()) { Return the appropriate data according to Formart $type = isset ($_request[' format ')? $_request[' format ']: '; Switch ($type) {case ' json ': self::response_json ($code, $message, $data); Break Case ' XML ': self::response_xml ($code, $message, $data); Break Case ' array ': echo var_dump (self::grant_array ($code, $message, $data)); Break Default:self::response_json ($code, $message, $data); Break }} public static function Response_json ($code, $message = ", $data =array ()) {//return JSON data $result = Self :: Gramt_array ($code, $message, $data); Echo Json_encode ($result); Exit (); public static function Response_xml ($code, $message = ", $data =array ()) {//return xml data $result = SelF::gramt_array ($code, $message, $data); $xml = "<?xml version= ' 1.0 ' encoding= ' UTF-8 '?>"; $xml. = "<root>"; $xml. = Self::xml_encode ($result); $xml. = "</root>" echo $xml; Exit (); } Private Function Xml_encode ($arr =array ()) {//for xml, You need to implement an XML encode method $xml = $attr = "; foreach ($arr as $key = + $value) {if (is_numberic ($key)) {$key = ' Item '; $attr = "id= ' {$key} '"; } $xml. = "<{$key} {$attr}>"; $xml. = (is_array ($value))? self::xml_encode ($value): $value; $xml. = "</{$key}>"; } return $xml; } Private Function Grant_array ($code, $message = ", $data =array ()) {////before all operations, you need to generate an array conforming to the API specification $result = { "code" = $code, "message" and "$message", "data" and "$data"; Return $result; }}</pre><p>The <p> Response class implements the data encapsulation of the communication interface and can flexibly write data in different formats to response according to the format specified in the Response. <br> Pay: full code and invocation instance: </p></p><pre><?php class response{/* * Sealed Communication interface Data * @param integer $code Status code * @param string $message status information * @param Array $data Data * Return string */public static function Api_response ($code, $message = ", $data =arra Y ()) {$type = isset ($_request[' format ')? $_request[' format ']: '; Switch ($type) {case ' json ': self::response_json ($code, $message, $data); Break Case ' XML ': self::response_xml ($code, $message, $data); Break Case ' array ': echo var_dump (self::grant_array ($code, $message, $data)); Break Default:self::response_json ($code, $message, $data); Break }/* * Package is for JSON data type * @param integer $code Status code * @param string $message status information * @param array $data data * Return string */public static function Json_response ($code, $message = ", $data=array ()) {$result = Self::grant_array ($code, $message, $data); Echo Json_encode ($result); Exit }/* * Number of packages for XML data type * @param integer $code Status code * @param string $message status information * @param array $data data * r Eturn string */public static function Xml_response ($code, $message = ", $data =array ()) {$result = SE Lf::grant_array ($code, $message, $data); Header ("content-type:text/xml"); $xml = "<?xml version= ' 1.0 ' encoding= ' UTF-8 '? >\n"; $xml. = "<root>\n"; $xml. = Self::xml_encode ($result); $xml. = "</root>"; Echo $xml; Exit (); }/* * Convert array to XML format * @param array $array Array * return String * * Private function Xml_encode ($array =array ()) {$xml = $attr = ""; If (!empty ($array)) {foreach ($array as $key = + $value) {if (is_numeric ($key)) { $attr = "id= ' {$key} '"; $key = "item"; } $xml. = "<{$key} {$attr}>"; $xml. = Is_array ($value)? Self::xml_encode ($value): $value; $xml. = "</{$key}>\n"; }} return $xml; */* * Generate the original data array according to the interface format * @param integer $code Status code * @param string $message status information * @param array $data data * Return array */private function Grant_array ($code, $message = ", $data =array ()) {if (!is_numeric ($code)) { Return '; } $result = array (' code ' = = $code, ' message ' = $message, ' data ' = $dat a); Return $result; }}</pre><p><p>Invoke Instance:</p></p><pre><pre><?php require_once "./databaseutil.class.php"; require_once "./response.class.php"; $connect = databaseutil:: getinstance ()->connect ();/** Home interface * http://domain/hotgirl/callback.php?format=xml/json*/$page = Isset ($_REQUEST[ ' Page '])? $_request[' page ']:1; $page _number = isset ($_request[' page_number '])? $_request[' page_number ']:2; $lim _ Start = ($page-1) * $page _number; $sql = "select * from user WHERE is_active=1 LIMIT". $lim _start. ",". $page _number; $result = mysql_query ($sql, $connect), if ($result && mysql_num_rows ($result) >0) {while ($row = Mysql_fetch_ Assoc ($result)) { $rows [] = $row; }} If (!empty ($ROWS)) { return response::api_response ($, ' Success ', $rows);} else{ return response::api_response (403, ' No result from database ');}</pre></pre><p><p>Robot fetch source: Go to the original website</p></p><p><p>PS: Crawl This article is because I recognize this scheme</p></p><br>Original address: using PHP to write an app API<p><p>Use PHP to write an app API</p></p></span>
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