第一部分,通信接口的實(shí)現(xiàn)
標(biāo)簽(空格分隔): PHP 手機(jī)后臺(tái) api 通信接口 Andy
PHP是世界上最強(qiáng)大的語(yǔ)言哪廓。
PHP開發(fā)手機(jī)API時(shí)泛领,一般返回XML或JSON數(shù)據(jù)類型的數(shù)據(jù),除了要返回從源數(shù)據(jù)(程序本身需要的數(shù)據(jù))外還應(yīng)附上狀態(tài)碼号杠,以下是一段封裝后的數(shù)據(jù)闭树,它使用JSON格式展現(xiàn):
{
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格式數(shù)據(jù)的實(shí)例:
<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>
此外耸棒,為了方便手機(jī)端開發(fā)人員的調(diào)試,還可直接返回帶數(shù)據(jù)類型的數(shù)據(jù):
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"
}
}
}
如何實(shí)現(xiàn)
當(dāng)手機(jī)客戶端通過API獲取數(shù)據(jù)時(shí)报辱,PHP腳本會(huì)Response
一個(gè)數(shù)組榆纽,并對(duì)這個(gè)數(shù)組進(jìn)行encode
,他們分別是json
,xml
和array
;該數(shù)組,定義如下:
$result = [
"code" => 200,
"message" => "數(shù)據(jù)返回成功",
"data" => ["key"=>"value", "key"=>"value"]
]
其中奈籽,code
代表狀態(tài)碼,message
代表狀態(tài)信息鸵赫,data
是程序邏輯中需要的數(shù)據(jù)衣屏。
如何去設(shè)計(jì)
當(dāng)手機(jī)端調(diào)用API,程序業(yè)務(wù)邏輯處理完成之后辩棒,需要返回?cái)?shù)據(jù)狼忱,此時(shí)需要對(duì)通信數(shù)據(jù)進(jìn)行封裝,封裝的三種類型由可由REQUEST
里的format
參數(shù)指定一睁,當(dāng)formart=json
時(shí)執(zhí)行response_json
方法钻弄,同理,還有response_xml
和response_array
方法者吁;
為了方便調(diào)用窘俺,編寫一個(gè)Response
類來(lái)封裝數(shù)據(jù)并完成response
工作:
/*本段代碼沒有經(jīng)過實(shí)際環(huán)境測(cè)試,也沒有嚴(yán)謹(jǐn)?shù)膮?shù)檢查*/
class Response{
public static function response_api($code, $message='', $data=array()){
//根據(jù)formart返回適當(dāng)?shù)臄?shù)據(jù)
$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數(shù)據(jù)
$result = self::gramt_array($code,$message,$data);
echo json_encode($result);
exit();
}
public static function response_xml($code, $message='', $data=array()){
//返回XML數(shù)據(jù)
$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()){
//對(duì)于XML复凳,需要自己實(shí)現(xiàn)一個(gè)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規(guī)范的數(shù)組
$result = {
"code" => $code,
"message" => $message,
"data" => $data
};
return $result;
}
}
Response類實(shí)現(xiàn)了通信接口的數(shù)據(jù)封裝,可根據(jù)response
內(nèi)指定的format靈活的寫入不同格式的數(shù)據(jù)到Response育八。
付:完整代碼及調(diào)用實(shí)例:
<?php
class Response{
/*
* 封通信接口數(shù)據(jù)
* @param integer $code 狀態(tài)碼
* @param string $message 狀態(tài)信息
* @param array $data 數(shù)據(jù)
* 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;
}
}
/*
* 封裝數(shù)為為json數(shù)據(jù)類型
* @param integer $code 狀態(tài)碼
* @param string $message 狀態(tài)信息
* @param array $data 數(shù)據(jù)
* return string
*/
public static function json_response($code,
$message='', $data=array()){
$result = self::grant_array($code, $message, $data);
echo json_encode($result);
exit;
}
/*
* 封裝數(shù)為為xml數(shù)據(jù)類型
* @param integer $code 狀態(tài)碼
* @param string $message 狀態(tài)信息
* @param array $data 數(shù)據(jù)
* 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();
}
/*
* 將數(shù)組轉(zhuǎn)換為XML格式
* @param array $array 數(shù)組
* 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;
}
/*
* 按照接口格式生成原數(shù)據(jù)數(shù)組
* @param integer $code 狀態(tài)碼
* @param string $message 狀態(tài)信息
* @param array $data 數(shù)據(jù)
* 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;
}
}
調(diào)用實(shí)例:
<?php
require_once "./DataBaseUtil.class.php";
require_once "./Response.class.php";
$connect = DataBaseUtil::getInstance()->connect();
/*
* 首頁(yè)接口
* 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');
}