第一部分,通信接口的实现
标签(空格分隔): PHP 手机后台 api 通信接口 Andy
PHP开发手机API时,一般返回XML或JSON数据类型的数据,除了要返回从源数据(程序本身需要的数据)外还应附上状态码,以下是一段封装后的数据,它使用JSON格式展现:
{ code: 200, message: "Success", data: [ { username: "安明哲", password: "123456", level: "1", gold: "0", id: "6", create_time: "2015-09-26 08:25:31", is_active: "1", is_admin: "0" }, { username: "张三", password: "12345", level: "1", gold: "0", id: "8", create_time: "0000-00-00 00:00:00", is_active: "1", is_admin: "0" } ] }
以下是XML格式数据的实例:
<root> <code>200</code> <message>Success</message> <data> <item id="0"> <username>安明哲</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>张三</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>
此外,为了方便手机端开发人员的调试,还可直接返回带数据类型的数据:
array(3) { ["code"]=> int(200) ["message"]=> string(7) "Success" ["data"]=> array(2) { [0]=> array(8) { ["username"]=> string(9) "安明哲" ["password"]=> string(6) "123456" ["level"]=> string(1) "1" ["gold"]=> string(1) "0" ["id"]=> string(1) "6" ["create_time"]=> string(19) "2015-09-26 08:25:31" ["is_active"]=> string(1) "1" ["is_admin"]=> string(1) "0" } [1]=> array(8) { ["username"]=> string(6) "张三" ["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" } } }
如何实现
当手机客户端通过API获取数据时,PHP脚本会Response一个数组,并对这个数组进行encode,他们分别是json,xml和array;该数组,定义如下:
$result = [
"code" => 200,
"message" => "数据返回成功",
"data" => ["key"=>"value", "key"=>"value"]
]
其中,code代表状态码,message代表状态信息,data是程序逻辑中需要的数据。
如何去设计
当手机端调用API,程序业务逻辑处理完成之后,需要返回数据,此时需要对通信数据进行封装,封装的三种类型由可由REQUEST里的format参数指定,当formart=json时执行response_json方法,同理,还有response_xml和response_array方法;
为了方便调用,编写一个Response类来封装数据并完成response工作:
/*本段代码没有经过实际环境测试,也没有严谨的参数检查*/ class Response{ public static function response_api($code, $message=‘‘, $data=array()){ //根据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()){ //返回JSON数据 $result = self::gramt_array($code,$message,$data); echo json_encode($result); exit(); } public static function response_xml($code, $message=‘‘, $data=array()){ //返回XML数据 $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()){ //对于XML,需要自己实现一个XML的encode方法 $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()){ //在所有操作之前,需要生成符合API规范的数组 $result = { "code" => $code, "message" => $message, "data" => $data }; return $result; } }
Response类实现了通信接口的数据封装,可根据response内指定的format灵活的写入不同格式的数据到Response。
付:完整代码及调用实例:
<?php class Response{ /* * 封通信接口数据 * @param integer $code 状态码 * @param string $message 状态信息 * @param array $data 数据 * return string */ public static function api_response($code, $message=‘‘, $data=array()){ $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; } } /* * 封装数为为json数据类型 * @param integer $code 状态码 * @param string $message 状态信息 * @param array $data 数据 * return string */ public static function json_response($code, $message=‘‘, $data=array()){ $result = self::grant_array($code, $message, $data); echo json_encode($result); exit; } /* * 封装数为为xml数据类型 * @param integer $code 状态码 * @param string $message 状态信息 * @param array $data 数据 * return string */ public static function xml_response($code, $message=‘‘, $data=array()){ $result = self::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(); } /* * 将数组转换为XML格式 * @param 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; } /* * 按照接口格式生成原数据数组 * @param integer $code 状态码 * @param string $message 状态信息 * @param array $data 数据 * return array */ private function grant_array($code, $message=‘‘, $data=array()){ if(!is_numeric($code)){ return ‘‘; } $result = array( ‘code‘ => $code, ‘message‘ => $message, ‘data‘ => $data ); return $result; } }
调用实例:
<?php require_once "./DataBaseUtil.class.php"; require_once "./Response.class.php"; $connect = DataBaseUtil::getInstance()->connect(); /* * 首页接口 * 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(200, ‘Success‘, $rows); } else{ return Response::api_response(403, ‘No result from database‘); }
Robot抓取来源:去原网站
PS:抓取这篇文章是由于本人认可这种方案
原文地址:用PHP编写一个APP的API
时间: 2024-10-21 03:37:01