1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Xmly; |
4
|
|
|
|
5
|
|
|
final class Util |
6
|
|
|
{ |
7
|
4 |
|
public static function jsonDecode($json, $assoc = false, $depth = 512) |
8
|
|
|
{ |
9
|
4 |
|
static $jsonErrors = array( |
10
|
|
|
JSON_ERROR_DEPTH => 'JSON_ERROR_DEPTH - Maximum stack depth exceeded', |
11
|
|
|
JSON_ERROR_STATE_MISMATCH => 'JSON_ERROR_STATE_MISMATCH - Underflow or the modes mismatch', |
12
|
|
|
JSON_ERROR_CTRL_CHAR => 'JSON_ERROR_CTRL_CHAR - Unexpected control character found', |
13
|
|
|
JSON_ERROR_SYNTAX => 'JSON_ERROR_SYNTAX - Syntax error, malformed JSON', |
14
|
|
|
JSON_ERROR_UTF8 => 'JSON_ERROR_UTF8 - Malformed UTF-8 characters, possibly incorrectly encoded' |
15
|
|
|
); |
16
|
|
|
|
17
|
4 |
|
if (empty($json)) { |
18
|
|
|
return null; |
19
|
|
|
} |
20
|
4 |
|
$data = \json_decode($json, $assoc, $depth); |
21
|
|
|
|
22
|
4 |
|
if (JSON_ERROR_NONE !== json_last_error()) { |
23
|
|
|
$last = json_last_error(); |
24
|
|
|
throw new \InvalidArgumentException( |
25
|
|
|
'Unable to parse JSON data: ' |
26
|
|
|
. (isset($jsonErrors[$last]) |
27
|
|
|
? $jsonErrors[$last] |
28
|
|
|
: 'Unknown error') |
29
|
|
|
); |
30
|
|
|
} |
31
|
|
|
|
32
|
4 |
|
return $data; |
33
|
|
|
} |
34
|
|
|
|
35
|
4 |
|
public static function randomString($length = 11) |
36
|
|
|
{ |
37
|
4 |
|
$str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890'; |
38
|
4 |
|
$randStr = str_shuffle(str_repeat($str, $length));//打乱字符串 |
39
|
4 |
|
return substr($randStr, 0, $length); |
40
|
|
|
} |
41
|
|
|
|
42
|
4 |
|
public static function msecTime() |
43
|
|
|
{ |
44
|
4 |
|
return floor(microtime(true) * 1000); |
45
|
|
|
} |
46
|
|
|
|
47
|
1 |
|
public static function mkdir() |
48
|
|
|
{ |
49
|
1 |
|
if (!is_dir(Config::LOG_PATH)) { |
50
|
1 |
|
mkdir(Config::LOG_PATH, 0777, true); |
51
|
|
|
} |
52
|
1 |
|
} |
53
|
|
|
|
54
|
1 |
|
public static function writeLog($logLevel, $url, $body, $statusCode = null, $duration = null, $error = null) |
55
|
|
|
{ |
56
|
1 |
|
self::mkdir(); |
57
|
1 |
|
date_default_timezone_set("Asia/Shanghai"); |
58
|
1 |
|
$logFileName = Config::LOG_PATH . $logLevel . '-' . date('Y-m-d-H') . '.log'; |
59
|
1 |
|
if ($logLevel === 'INFO') { |
60
|
|
|
$content = date('Y-m-d H:i:s') . ' [' . $logLevel . '] '; |
61
|
|
|
$content .= $statusCode . ' ' . $duration . ' ' . $url . ' ' . $body . "\n"; |
62
|
|
|
} else { |
63
|
1 |
|
$content = date('Y-m-d H:i:s') . ' [' . $logLevel . '] '; |
64
|
1 |
|
$content .= $statusCode . ' ' . $duration . ' ' . $url . ' ' . $body . ' ' . $error . "\n"; |
65
|
|
|
} |
66
|
|
|
|
67
|
1 |
|
file_put_contents($logFileName, $content, FILE_APPEND); |
68
|
1 |
|
} |
69
|
|
|
|
70
|
|
|
public static function writeInfoLog($url, $body, $statusCode, $duration) |
71
|
|
|
{ |
72
|
|
|
self::writeLog('INFO', $url, $body, $statusCode, $duration); |
73
|
|
|
} |
74
|
|
|
|
75
|
1 |
|
public static function writeErrLog($url, $body, $statusCode, $duration, $error) |
76
|
|
|
{ |
77
|
1 |
|
self::writeLog('ERROR', $url, $body, $statusCode, $duration, $error); |
78
|
1 |
|
} |
79
|
|
|
} |
80
|
|
|
|