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

json_encode(

math?formula=json%2Coption) ,$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

下载日志

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;

}

查看下载下来的日志

e5d71dadddf5

81217172018.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值