THINKPHP5开发API笔记

2017-08-14  本文已影响0人  7abbcd54a89d

1.解决跨域的析构方法

protected  function _initialize()
    {
        header('content-type:text/html;charset=utf-8');
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Header:Origin,X-Requested-With,Content-Type,Accept');
        header('Access-Control-Allow-Methods:GET,POST,PUT');
        ksort($_POST);
        ksort($_GET);
    }

2.生成token

public  function creat_token()
    {
       $token=md5(mt_rand(111111,999999) . time());
       return $token;
    }

3.根据token判断用户身份

public  function creat_token()
    {
      $token = trim($_POST['token']);
        $mid = Db::name('token')->where("token='{$token}'")->value('mid');
        if ($mid > 0) {
            return $mid;
        } else {
            ajax_msg('非法数据', 0);
        }
    }

4.API数据返回接口

 public function ajax_msg($msg = "", $status = 1, $data = "", $errcode = "")
    {
        $json['$msg'] = $msg;
        $json['$status'] = $status;
        $json['$data'] = $data;
        if ($errcode) {
            $json['$errcode'] = $errcode;
        }
        echo json_encode($json, true);
        exit;
    }
上一篇下一篇

猜你喜欢

热点阅读