网站首页 > 代码片段 > Php > 正文

php api接口应用类框架

2017-08-26 22:20:19 来源: 点击:站内收藏
适用于服务器端api接口类应用的小框架,完整源码包下载:http://www.yikoo.net
 
<?php
  
error_reporting(E_ERROR);
  
define('API', 'api');
define('DOCROOT',__DIR__);
define('API_SECRET','yikoo.net'); // 根据需要修改
  
//兼容各种提交方式,get、post、字节流
$data1 = $_GET;
$data2=file_get_contents("php://input");
parse_str($data2, $data2);
$data = array_merge($data2, $data1);
unset($data1,$data2);
  
//接口权限验证
$token = md5($data['time'] . API_SECRET);
if (!isset($data['token']) || $token != $data['token']) {
    exit('token error');
}
  
if ( !isset($data['action']) || !$data['action'] ) {
    exit('action error');
}
  
foreach ($data as $k => $v) {
    $v = trim($v);
    $v = strip_tags($v);
    $v = addslashes($v);
    $data[$k] = $v;
}
  
$data['action']=explode('_',$data['action']);
  
try{
    include DOCROOT . '/model/' . $data['action'][0] . '/' . $data['action'][0] . '.php';
    $api = new $data['action'][0]($data);
    $api->$data['action'][1]();
}
catch (Exception $e) {
    echo $e->getMessage();
}
  
  
class api {
  
    public $data = array();
  
    function __construct($data) {
        //根据需要,在此加载公用资源;
        $this->data = $data;
    }
  
}
  
?>

相关热词搜索:api 框架
评论正在载入