json_encode($json,$option) 对变量进行 JSON 编码说明

json_encode(option) ,$option参数为非必填
原数据 一组异常日志

array:4 [▼
  0 => array:4 [▼
    "note" => "微信通知发送考试信息异常"
    "params" => "{"response":{},"formattedResponse":{"errcode":40164,"errmsg":"invalid ip 119.130.171.198, not in whitelist hint: [oA89HA06741501]"}}"
    "ip" => 2130706433
    "created_at" => "2018-12-12 15:11:13"
  ]
  1 => array:4 [▼
    "note" => "微信通知发送考试信息异常"
    "params" => "{}"
    "ip" => 2130706433
    "created_at" => "2018-11-30 17:14:52"
  ]
  2 => array:4 [▼
    "note" => "邮箱通知发送考试信息异常"
    "params" => "{}"
    "ip" => 2130706433
    "created_at" => "2018-11-30 17:12:07"
  ]
  3 => array:4 [▶]
]

demo,未传参时取得数据$json为

$logs = json_encode($json);//
"[{"note":"\u5fae\u4fe1\u901a\u77e5\u53d1\u9001\u8003\u8bd5\u4fe1\u606f\u5f02\u5e38","params":"{\"response\":{},\"formattedResponse\":{\"errcode\":40164,\"errmsg\":\"invalid ip 119.130.171.198, not in whitelist hint: [oA89HA06741501]\"}}","ip":2130706433,"created_at":"2018-12-12 15:11:13"},{"note":"\u5fae\u4fe1\u901a\u77e5\u53d1\u9001\u8003\u8bd5\u4fe1\u606f\u5f02\u5e38","params":"{}","ip":2130706433,"created_at":"2018-11-30 17:14:52"},{"note":"\u90ae\u7bb1\u901a\u77e5\u53d1\u9001\u8003\u8bd5\u4fe1\u606f\u5f02\u5e38","params":"{}","ip":2130706433,"created_at":"2018-11-30 17:12:07"},{"note":"\u5fae\u4fe1\u901a\u77e5\u53d1\u9001\u8003\u8bd5\u4fe1\u606f\u5f02\u5e38","params":"{}","ip":2130706433,"created_at":"2018-11-30 17:12:06"}] ◀"

这种格式不方便查看日志,可用以下参数格式化,用空白字符格式化返回的数据。 自 PHP 5.4.0 起生效。

$logs = json_encode($json,JSON_PRETTY_PRINT);//128
//取得数据为
"""
[\n
    {\n
        "note": "\u5fae\u4fe1\u901a\u77e5\u53d1\u9001\u8003\u8bd5\u4fe1\u606f\u5f02\u5e38",\n
        "params": "{\"response\":{},\"formattedResponse\":{\"errcode\":40164,\"errmsg\":\"invalid ip 119.130.171.198, not in whitelist hint: [oA89HA06741501]\"} ▶
        "ip": 2130706433,\n
        "created_at": "2018-12-12 15:11:13"\n
    },\n
    {\n
        "note": "\u5fae\u4fe1\u901a\u77e5\u53d1\u9001\u8003\u8bd5\u4fe1\u606f\u5f02\u5e38",\n
        "params": "{}",\n
        "ip": 2130706433,\n
        "created_at": "2018-11-30 17:14:52"\n
    },\n
    {\n
        "note": "\u90ae\u7bb1\u901a\u77e5\u53d1\u9001\u8003\u8bd5\u4fe1\u606f\u5f02\u5e38",\n
        "params": "{}",\n
        "ip": 2130706433,\n
        "created_at": "2018-11-30 17:12:07"\n
    },\n
    {\n
        "note": "\u5fae\u4fe1\u901a\u77e5\u53d1\u9001\u8003\u8bd5\u4fe1\u606f\u5f02\u5e38",\n
        "params": "{}",\n
        "ip": 2130706433,\n
        "created_at": "2018-11-30 17:12:06"\n
    }\n
]
"""

但是中文提示被转码,避免该情况可用,以字面编码多字节 Unicode 字符 自 PHP 5.4.0 起生效

$logs = json_encode($json,JSON_UNESCAPED_UNICODE);//256
\\取得数据为
"[{"note":"微信通知发送考试信息异常","params":"{\"response\":{},\"formattedResponse\":{\"errcode\":40164,\"errmsg\":\"invalid ip 119.130.171.198, not in whitelist hint: [oA89HA06741501]\"}}","ip":2130706433,"created_at":"2018-12-12 15:11:13"},{"note":"微信通知发送考试信息异常","params":"{}","ip":2130706433,"created_at":"2018-11-30 17:14:52"},{"note":"邮箱通知发送考试信息异常","params":"{}","ip":2130706433,"created_at":"2018-11-30 17:12:07"},{"note":"微信通知发送考试信息异常","params":"{}","ip":2130706433,"created_at":"2018-11-30 17:12:06"}] ◀"

同时使用两个参数或者多个

$logs = json_encode($json,JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT);//128|256
//或者$logs = json_encode($json,128|256);//
//取得数据
"""
[\n
    {\n
        "note": "微信通知发送考试信息异常",\n
        "params": "{\"response\":{},\"formattedResponse\":{\"errcode\":40164,\"errmsg\":\"invalid ip 119.130.171.198, not in whitelist hint: [oA89HA06741501]\"} ▶
        "ip": 2130706433,\n
        "created_at": "2018-12-12 15:11:13"\n
    },\n
    {\n
        "note": "微信通知发送考试信息异常",\n
        "params": "{}",\n
        "ip": 2130706433,\n
        "created_at": "2018-11-30 17:14:52"\n
    },\n
    {\n
        "note": "邮箱通知发送考试信息异常",\n
        "params": "{}",\n
        "ip": 2130706433,\n
        "created_at": "2018-11-30 17:12:07"\n
    },\n
    {\n
        "note": "微信通知发送考试信息异常",\n
        "params": "{}",\n
        "ip": 2130706433,\n
        "created_at": "2018-11-30 17:12:06"\n
    }\n
]
带参可直接用对应数字
    JSON_HEX_TAG => 1
    JSON_HEX_AMP => 2
    JSON_HEX_APOS => 4
    JSON_HEX_QUOT => 8
    JSON_FORCE_OBJECT => 16
    JSON_NUMERIC_CHECK => 32
    JSON_UNESCAPED_SLASHES => 64
    JSON_PRETTY_PRINT => 128
    JSON_UNESCAPED_UNICODE => 256
    JSON_PARTIAL_OUTPUT_ON_ERROR => 512
    JSON_PRESERVE_ZERO_FRACTION => 1024

详细文档link http://php.net/manual/zh/json.constants.php

下载日志

public function downLoadLogs()
    {
        $query = ErrorLogModel::query()->select('note', 'params','ip','created_at')->orderBy('id','desc')->lockForUpdate()->get();

        $logs = json_encode($query->toArray(),256|128);
        // $logs = json_encode($query->toArray(),JSON_UNESCAPED_UNICODE);//256
        // $logs = json_encode($query->toArray(),JSON_PRETTY_PRINT);//128
        file_put_contents('error.log', $logs);
        $file = 'error.log';
        if(file_exists($file)){
            header("Content-type:application/octet-stream");
            $filename = basename($file);
            header("Content-Disposition:attachment;filename = ".$filename);
            header("Accept-ranges:bytes");
            header("Accept-length:".filesize($file));
            readfile($file);
        }else{
            echo "";
        }
        exit;
    }

查看下载下来的日志


json_encode($json,$option) 对变量进行 JSON 编码说明_第1张图片
81217172018.png

你可能感兴趣的:(json_encode($json,$option) 对变量进行 JSON 编码说明)