thinkphp6的api服务器简约探针


一个简单的thinkphp6 api接口简约探针,可以获取:

文件缓存读写检查

REDIS缓存读写检查

mysql或mariadb的查询权限

日志模块权限功能检查

php的时区和数据库时区等非机密的常用参数

<?php
declare (strict_types=1);

namespace app\index\controller;

class Index
{
    public function index()
    {
        try {
            $probing = \think\facade\Cache::store('file')->get('probing');
        } catch (\Exception $e) {
            return $this->json(-1, 'Cache Failed');
        }

        if ($probing) {
            return $this->json(0, 'Cache Info', $probing);
        }

        //phpinfo();die;
        $info = [];
        $info['APP_DEBUG'] = env('APP_DEBUG');
        $info['APP_ENV'] = env('APP_ENV');

        #mysql check
        $mysql = 'ok';
        try {
            \think\facade\Db::query('SELECT md5("xxx")');
        } catch (\PDOException $e) {
            $mysql = $e->getMessage();
        }

        #redis check
        $redis = 'ok';
        try {
            \think\facade\Cache::store('redis')->set('name', 'value', 3600);
        } catch (\RedisException $e) {
            $redis = $e->getCode();
        }//dump($redis);

        //runtime folder excpetion can not be cache
        #cache check
        $cache = 'ok';
        try {
            \think\facade\Cache::store('file')->set('name', 'value', 3600);
        } catch (\Exception $e) {
            $cache = $e->getMessage();//HTTP ERROR 500
        }

        #log check
        $log = 'ok';
        try {
            \think\facade\Log::write('test log');
        } catch (\Exception $e) {
            $log = $e->getMessage();
        }

        $info['CONNECT_MYSQL'] = $mysql;
        $info['CONNECT_REDIS'] = $redis;
        $info['CACHE_STATUS'] = $cache;
        $info['LOG_STATUS'] = $log;

        //halt($_SERVER);
        $info['HTTP_X_REAL_IP'] = $_SERVER['HTTP_X_REAL_IP'] ?? '-';
        $info['REMOTE_ADDR'] = $_SERVER['REMOTE_ADDR'] ?? '-';
        $info['PHP_DATETIME'] = date('Y-m-d H:i:s');
        $info['PHP_TIMEZONE'] = date_default_timezone_get();
        $info['MYSQL_DATETIME'] = current(current(\think\facade\Db::query('SELECT NOW();')));
        $info['MYSQL_TIMEZONE'] = implode('/', array_column(\think\facade\Db::query('show variables LIKE "%time_zone%";'), 'Value'));

        //记入缓存
        \think\facade\Cache::store('file')->set('probing', $info, 3);

        //LAST.bykc20201111
        return $this->json(0, sprintf('hey.%s', time()), $info);
    }
}

然后父类(控制器基础类)abstract class BaseController可以有两个json返回函数:

    /**
     * CMD结束json输出
     * @param int $code 业务数据状态
     * @param string $msg 业务数据出错后的提示,未出错则未空字串
     * @param array $data 业务数据
     */
    protected function finish(int $code, string $msg, array $data = [])
    {
        $res = $this->json($code, $msg, $data);
        //$res->setSession($this->app->session);
        $res->send();
        $this->app->http->end($res);
        exit;
    }

    /**
     * CMD抽象json输出
     * @param int $code
     * @param string $msg
     * @param array $data
     * @return \think\response\Json
     */
    protected function json(int $code, string $msg, array $data = [])
    {
        if (empty($data)) {
            $data = new \stdClass();//为空硬是搞成空对象,确保data是对象
        }
        if (is_array($data) and isset($data[0])) {
            $code = $code + 10000;//确保是json对象而不是json数组!
            $data = ['error' => 'parent.json(...)', 'msg' => $msg, 'preview' => substr(json_encode($data), 0, 20) . '..'];
            $msg = '[BaseController.json()]param data require object,an Array given';
        }
        return json(['code' => $code, 'msg' => $msg, 'data' => $data]);
    }

原文链接:https://blog.yongit.com/note/713397.html