首先添加一个接口的基础控制器,用于把数据转化为需要的格式(json,xml),可以命名为ApiBaseController ,
<?php
// +----------------------------------------------------------------------
// | TEA [ The customer with the product, we won't change any day. ]
// +----------------------------------------------------------------------
// | Copyright (c) 2015 http://www.lechezai.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Elinx <654753115@qq.com> Last modified time:2016-01-02
// +----------------------------------------------------------------------
namespace Home\Controller;
use Think\Controller;
/**
* Api 公共控制器
*/
class ApiBaseController extends Controller {
// private $appid = ''; // 签名id
// private $appsecret = ''; // 签名值
// public function _empty() {//空操作
// $this->apiReturn(4001,'invalid action');
// }
// // 初始化服务端接口
// protected function _initialize() {
// if (ACTION_NAME != 'gettoken') {
// //签名认证
// $this->checkSignature();
// }
// }
/**
@@ 窝是没有意思的分隔线 @_@
*/
/**
* 签名认证
*/
private function checkSignature() {
// $_GET['token'] = '4c8f01d06e1735f88d1a97f63c87c9150a95c864';
if(!isset($_GET['token'])){
$this->apiReturn(4001,'invalid token');
} else if (!S($_GET['token'])){
$this->apiReturn(4001,'invalid token');
}
}
// 获取token
public function gettoken(){
$appid = I('appid'); // 签名id
$appsecret = I('appsecret'); // 签名值
//检查是否为空
if (empty($appid) || empty($appsecret)) {
$this->apiReturn(4001,'need appid or appsecrect');
}
// 获取远程api数据
$api = M('api')->where(array('api_appid'=>$appid, 'api_appsecret'=>$appsecret))->find();
// 判断appid、appsecret是否合法
if (!$api) {
$this->apiReturn(4001,'invalid!');
} else {
// 判断是否被禁用
if ($api['status'] != 1) {
$this->apiReturn(300,'apikey invalid!');
} else {
$this->appid = $api['api_appid'];
$this->appsecret = $api['api_appsecret'];
}
}
// 获取缓存中token
$ori_str = S($this->appid.'_'.$this->appsecret);
if($ori_str){ // 不进行重新获取,把以前的token返回
$this->apiReturn(200, 'success', array('token'=>$ori_str));
// S($ori_str, null); // 重新获取并把原有的删除
} else {
//这里是token产生的机制 您也可以自己定义
$nonce = createNoncestr(32);
$tmpArr = array($nonce,$this->appid,$this->appsecret);
sort($tmpArr, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
// echo $tmpStr;
//这里做了缓存 'a'=>b 和'b'=>a格式的缓存
S($this->appid.'_'.$this->appsecret,$tmpStr,7200);
S($tmpStr,$this->appid.'_'.$this->appsecret,7200);// 缓存token值
//返回token值
$data = array('token'=>$tmpStr);
$this->apiReturn(200,'success',$data);
}
}
/**
* [apiReturn 用于给app提供接口使用 带有请求结果状态表示,和结果提示,默认返回json]
* @param [number] $status [请求结果的状态标识,设定后要在文档中给予说明]
* @param string $message [请求结果的提示语句]
* @param [array] $data [请求返回的数据,app前端需要的数据]
* @param [string] $type [要返回的数据类型,支持json,xml,默认返回json]
* @return [json或xml] [返回数据]
*/
protected function apiReturn($status,$message='',$data=null,$type='json'){
if(!is_numeric($status) || !is_string($message) ){
$this->apiReturn('400','参数错误');
}
$res = array();
$res['status'] = $status;
$res['message'] = $message;
$res['data'] = $data;
if(in_array($type, array('json','xml'))){
$this->ajaxReturn($res,$type);
}else{
$this->ajaxReturn($res);
}
}
// 加载全局配置信息
protected function load_config(){
$attr = M('config_attr')->field('attr_title,config_value')->select();
foreach ($attr as $key => $value) {
$config[$value['attr_title']] = $value['config_value'];
}
return $config;
}
}
然后写接口的控制器继承ApiBaseController 这个控制器,
把需要的数据查出来后用apiReturn函数返回就ok了。
需要注意的是$this->apiReturn后,这个方法后面的代码都不执行了。