Thinkphp框架開發(fā)移動端接口(1)
更新時(shí)間:2016年08月18日 11:33:56 作者:zqwang121
這篇文章主要為大家詳細(xì)介紹了Thinkphp框架開發(fā)移動端接口,具有一定的實(shí)用性,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了使用Thinkphp框架開發(fā)移動端接口代碼,給原生APP提供api接口,具體內(nèi)容如下
1. 使用TP框架時(shí) 放在common文件夾下文件名就叫function.php
<?php
/**
* Created by zhangkx
* Email: zkx520tnhb@163.com
* Date: 2015/8/1
* Time: 23:15
*/
/*************************** api開發(fā)輔助函數(shù) **********************/
/**
* @param null $msg 返回正確的提示信息
* @param flag success CURD 操作成功
* @param array $data 具體返回信息
* Function descript: 返回帶參數(shù),標(biāo)志信息,提示信息的json 數(shù)組
*
*/
function returnApiSuccess($msg = null,$data = array()){
$result = array(
'flag' => 'Success',
'msg' => $msg,
'data' =>$data
);
print json_encode($result);
}
/**
* @param null $msg 返回具體錯(cuò)誤的提示信息
* @param flag success CURD 操作失敗
* Function descript:返回標(biāo)志信息 ‘Error',和提示信息的json 數(shù)組
*/
function returnApiError($msg = null){
$result = array(
'flag' => 'Error',
'msg' => $msg,
);
print json_encode($result);
}
/**
* @param null $msg 返回具體錯(cuò)誤的提示信息
* @param flag success CURD 操作失敗
* Function descript:返回標(biāo)志信息 ‘Error',和提示信息,當(dāng)前系統(tǒng)繁忙,請稍后重試;
*/
function returnApiErrorExample(){
$result = array(
'flag' => 'Error',
'msg' => '當(dāng)前系統(tǒng)繁忙,請稍后重試!',
);
print json_encode($result);
}
/**
* @param null $data
* @return array|mixed|null
* Function descript: 過濾post提交的參數(shù);
*
*/
function checkDataPost($data = null){
if(!empty($data)){
$data = explode(',',$data);
foreach($data as $k=>$v){
if((!isset($_POST[$k]))||(empty($_POST[$k]))){
if($_POST[$k]!==0 && $_POST[$k]!=='0'){
returnApiError($k.'值為空!');
}
}
}
unset($data);
$data = I('post.');
unset($data['_URL_'],$data['token']);
return $data;
}
}
/**
* @param null $data
* @return array|mixed|null
* Function descript: 過濾get提交的參數(shù);
*
*/
function checkDataGet($data = null){
if(!empty($data)){
$data = explode(',',$data);
foreach($data as $k=>$v){
if((!isset($_GET[$k]))||(empty($_GET[$k]))){
if($_GET[$k]!==0 && $_GET[$k]!=='0'){
returnApiError($k.'值為空!');
}
}
}
unset($data);
$data = I('get.');
unset($data['_URL_'],$data['token']);
return $data;
}
}
2. 查詢單個(gè)果品詳細(xì)信息
/**
* 發(fā)布模塊
*
* 獲取信息單個(gè)果品詳細(xì)信息
*
*/
public function getMyReleaseInfo(){
//檢查是否通過post方法得到數(shù)據(jù)
checkdataPost('id');
$where['id'] = $_POST['id'];
$field[] = 'id,fruit_name,high_price,low_price,address,size,weight,fruit_pic,remark';
$releaseInfo = $this->release_obj->findRelease($where,$field);
$releaseInfo['remark'] = mb_substr($releaseInfo['remark'],0,49,'utf-8').'...';
//多張圖地址按逗號截取字符串,截取后如果存在空數(shù)組則需要過濾掉
$releaseInfo['fruit_pic'] = array_filter(explode(',', $releaseInfo['fruit_pic']));
$fruit_pic = $releaseInfo['fruit_pic'];unset($releaseInfo['fruit_pic']);
//為圖片添加存儲路徑
foreach($fruit_pic as $k=>$v ){
$releaseInfo['fruit_pic'][] = 'http://'.$_SERVER['HTTP_HOST'].'/Uploads/Release/'.$v;
}
if($releaseInfo){
returnApiSuccess('',$releaseInfo);
}else{
returnApiError( '什么也沒查到(+_+)!');
}
}
3. findRelease() 方法的model
/**
* 查詢一條數(shù)據(jù)
*/
public function findRelease($where,$field){
if($where['status'] == '' || empty($where['status'])){
$where['status'] = array('neq','9');
}
$result = $this->where($where)->field($field)->find();
return $result;
}
4. app端接收到的數(shù)據(jù)(解碼json之后)
{
"flag": "success",
"message": "",
"responseList": {
"id": "2",
"fruit_name": "蘋果",
"high_price": "8.0",
"low_price": "5.0",
"address": "天津小白樓水果市場",
"size": "2.0",
"weight": "2.0",
"remark": "急需...",
"fruit_pic": [
"http://fruit.txunda.com/Uploads/Release/201508/55599e7514815.png",
"http://fruit.txunda.com/Uploads/Release/201508/554f2dc45b526.jpg"
]
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- Thinkphp5微信小程序獲取用戶信息接口的實(shí)例詳解
- 使用Thinkphp框架開發(fā)移動端接口
- ThinkPHP實(shí)現(xiàn)支付寶接口功能實(shí)例
- Thinkphp微信公眾號支付接口
- thinkPHP框架對接支付寶即時(shí)到賬接口回調(diào)操作示例
- thinkPHP微信分享接口JSSDK用法實(shí)例
- thinkPHP框架實(shí)現(xiàn)的短信接口驗(yàn)證碼功能示例
- Thinkphp框架開發(fā)移動端接口(2)
- ThinkPHP和UCenter接口沖突的解決方法
- ThinkPHP框架實(shí)現(xiàn)的微信支付接口開發(fā)完整示例
- thinkPHP5框架接口寫法簡單示例
相關(guān)文章
部署PHP項(xiàng)目應(yīng)該注意的幾點(diǎn)事項(xiàng)分享
這篇文章主要介紹了部署PHP項(xiàng)目應(yīng)該注意的幾點(diǎn)事項(xiàng),有需要的朋友可以參考一下2013-12-12

