php app介面實現(json和xml)

2021-07-04 12:47:40 字數 2588 閱讀 6680

上篇學習了封裝mysql的例項化物件類!

xml:擴充套件標記語言:可以標記資料 ,定義資料型別;資料格式清晰明了, 可讀性高;

json:一種輕量級的資料交換格式;生成資料簡單;傳輸速度快;

獲取資料:從資料庫中或者快取中獲取資料(可以是快取裡的資料)

提交資料:通過get方式或者post方式提交資料,服務端處理後,返回資料

狀態碼:code 

提示資訊:message 

返回資料:  data

快取 :靜態快取 , memcache 和 redis 快取技術

定時任務 :corntab 

json方式封裝介面資料方法

函式json_encode();

只接收utf-8的編碼資料

組裝字串 (簡單)

使用系統的方法

<?php

//header("content-type:text/html;charset=utf-8");

class response

//如果url上傳參了,去引數的型別,否則取得預設值!

$type=isset($_get['type'])?$_get['type']:self::json;

$result=array(

'code'=>$code,

'msg'=>$msg,

'data'=>$data

);if($type=='json')elseif ($type=='xml')elseif ($type='array')

}/**

* 02.按json方式輸出 通訊資料

* @param int $code 狀態碼

* @param string $msg 提示資訊

* @param array $data 資料

* retrun string

*/public static function jsonencode($code,$msg='',$data=array())

$result=array(

'code'=>$code,

'msg'=>$msg,

'data'=>$data

);echo json_encode($result);

exit(); }

/*** 03.封裝xml 輸出通訊資料

* @param unknown $code

* @param unknown $msg

* @param unknown $data

*/public static function xmlencode($code,$msg='',$data=array())

$result=array(

'code'=>$code,

'msg'=>$msg,

'data'=>$data

);header("content-type:text/xml");

$xml="<?xml version='1.0' encoding='utf-8'?>";

$xml.="";

$xml.=self::xmltoencode($result);

$xml.="";

echo $xml;

exit(); }

/***04. 拼裝 xml資料

* @param array $data

* @return string

* 使用遞迴,判斷是不是陣列,是陣列繼續呼叫迴圈

* xml的 節點不能為 數字,用item代替

*/public static function xmltoencode($data)'";

$key="item";

}$xml.="< >";

$xml.=is_array($value)?self::xmltoencode($value):$value;

$xml.="";

} return $xml; }

}?>

通過url實現 ,顯示資料型別 type可以為 xml ,json ,array !

test.php 實現如下:

$arr=array(

'id'=>1,

'name'=>'yuan',

'age'=>23,

'location'=>'hpu'

);$arr1=array(1,4,5,2,6,3);

response::jsonencode(200,'success',$arr);

//呼叫

$con=db::getinstance()->connect();

//查詢語句

$sql='select * from user_info';

//執行,返回結果集

$result=mysql_query($sql,$con);

//新增的新陣列

$arr3=array();

while ($row=mysql_fetch_row($result))

response::show('200','success',$arr3);

php app開發介面加密範例

inc 解析介面 客戶端介面傳輸規則 1.用cmd引數 base64 來動態呼叫不同的介面,介面位址統一為 2.將要傳過來的引數組成乙個陣列,陣列新增timestamp元素 當前時間戳,精確到秒 將陣列的鍵值按照自然排序從大到小排序 3.將陣列組成 key val key val的形式的字串,將字串...

tapestry 對外json介面實現

為了實現乙個對外的json介面,仔細讀了tapestry5.2的源 考慮了幾種實現,最簡便清晰的方法如下。為json介面做乙個page,例如命名為 jsonpage。在jsonpage裡只放乙個actonlink,給乙個命名例如 do jsonpage.tml如下 do 這樣外部訪問json介面ur...

json和xml封裝介面詳解

下面我們開始封裝json介面資料 class response result array code code,message message,data data 輸出json資料 echo json encode result exit xml封裝 class response result arr...