|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use GuzzleHttp\Client as HttpClient; |
|
4
|
|
|
|
|
5
|
|
|
if (! function_exists('urlsafe_base64_encode')) { |
|
6
|
|
|
/** |
|
7
|
|
|
* Encodes the given data with base64, and returns an URL-safe string. |
|
8
|
|
|
* |
|
9
|
|
|
* @param string $data |
|
10
|
|
|
* @return string |
|
11
|
|
|
*/ |
|
12
|
|
|
function urlsafe_base64_encode($data) |
|
13
|
|
|
{ |
|
14
|
|
|
return strtr(base64_encode($data), ['+' => '-', '/' => '_', '=' => '']); |
|
15
|
|
|
} |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
if (! function_exists('urlsafe_base64_decode')) { |
|
19
|
|
|
/** |
|
20
|
|
|
* Decodes a base64 encoded data. |
|
21
|
|
|
* |
|
22
|
|
|
* @param string $data |
|
23
|
|
|
* @param bool $strict |
|
24
|
|
|
* @return string |
|
25
|
|
|
*/ |
|
26
|
|
|
function urlsafe_base64_decode($data, $strict = false) |
|
27
|
|
|
{ |
|
28
|
|
|
return base64_decode(strtr($data.str_repeat('=', (4 - strlen($data) % 4)), '-_', '+/'), $strict); |
|
29
|
|
|
} |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
if (! function_exists('mb_trim')) { |
|
33
|
|
|
/** |
|
34
|
|
|
* Strip whitespace (or other characters) from the beginning and end of a string. |
|
35
|
|
|
* |
|
36
|
|
|
* @see https://github.com/vanderlee/PHP-multibyte-functions/blob/master/functions/mb_trim.php |
|
37
|
|
|
* |
|
38
|
|
|
* @param string $string |
|
39
|
|
|
* @return string |
|
40
|
|
|
*/ |
|
41
|
|
|
function mb_trim($string) |
|
42
|
|
|
{ |
|
43
|
|
|
return mb_ereg_replace('^\s*([\s\S]*?)\s*$', '\1', $string); |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
if (! function_exists('string_value')) { |
|
48
|
|
|
/** |
|
49
|
|
|
* Converts any type to a string. |
|
50
|
|
|
* |
|
51
|
|
|
* @param mixed $value |
|
52
|
|
|
* @param int $jsonOptions JSON_PRETTY_PRINT, etc |
|
53
|
|
|
* @return string |
|
54
|
|
|
*/ |
|
55
|
|
|
function string_value($value, $jsonOptions = 0) |
|
56
|
|
|
{ |
|
57
|
|
|
if (is_object($value)) { |
|
58
|
|
|
if (method_exists($value, '__toString')) { |
|
59
|
|
|
return (string) $value; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
if (method_exists($value, 'toArray')) { |
|
63
|
|
|
$value = $value->toArray(); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return is_string($value) ? $value : json_encode($value, $jsonOptions | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
if (! function_exists('http_client')) { |
|
72
|
|
|
/** |
|
73
|
|
|
* Create a Guzzle http client. |
|
74
|
|
|
* |
|
75
|
|
|
* @param array $config |
|
76
|
|
|
* @return \GuzzleHttp\Client |
|
77
|
|
|
*/ |
|
78
|
|
|
function http_client($config = []) |
|
79
|
|
|
{ |
|
80
|
|
|
return new HttpClient( |
|
81
|
|
|
array_merge([ |
|
82
|
|
|
'connect_timeout' => 5, |
|
83
|
|
|
'timeout' => 25, |
|
84
|
|
|
], $config) |
|
85
|
|
|
); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
if (! function_exists('fetch')) { |
|
90
|
|
|
/** |
|
91
|
|
|
* Request an URL. |
|
92
|
|
|
* |
|
93
|
|
|
* @param string $url |
|
94
|
|
|
* @param string $method |
|
95
|
|
|
* @param array $options |
|
96
|
|
|
* @return GuzzleHttp\Psr7\Response|null |
|
97
|
|
|
*/ |
|
98
|
|
|
function fetch($url, $method = 'GET', $options = []) |
|
99
|
|
|
{ |
|
100
|
|
|
try { |
|
101
|
|
|
return http_client()->request($method, $url, $options); |
|
102
|
|
|
} catch (Exception $e) { |
|
|
|
|
|
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
if (! function_exists('fetch_content')) { |
|
108
|
|
|
/** |
|
109
|
|
|
* Request an URL, and return the content. |
|
110
|
|
|
* |
|
111
|
|
|
* @param string $url |
|
112
|
|
|
* @param string $method |
|
113
|
|
|
* @param array $options |
|
114
|
|
|
* @param GuzzleHttp\Psr7\Response|null &$response |
|
115
|
|
|
* @return string |
|
116
|
|
|
*/ |
|
117
|
|
|
function fetch_content($url, $method = 'GET', $options = [], &$response = null) |
|
118
|
|
|
{ |
|
119
|
|
|
if ($response = fetch($url, $method, $options)) { |
|
120
|
|
|
return (string) $response->getBody(); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
if (! function_exists('fetch_json')) { |
|
126
|
|
|
/** |
|
127
|
|
|
* Request an URL, and return the JSON data. |
|
128
|
|
|
* |
|
129
|
|
|
* @param string $url |
|
130
|
|
|
* @param string $method |
|
131
|
|
|
* @param array $options |
|
132
|
|
|
* @param GuzzleHttp\Psr7\Response|null &$response |
|
133
|
|
|
* @return mixed |
|
134
|
|
|
*/ |
|
135
|
|
|
function fetch_json($url, $method = 'GET', $options = [], &$response = null) |
|
136
|
|
|
{ |
|
137
|
|
|
array_set($options, 'headers.Accept', 'application/json'); |
|
138
|
|
|
|
|
139
|
|
|
return json_decode(fetch_content($url, $method, $options, $response), true); |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
if (! function_exists('request')) { |
|
144
|
|
|
/** |
|
145
|
|
|
* Get an instance of the current request or an input item from the request. |
|
146
|
|
|
* |
|
147
|
|
|
* @param array|string $key |
|
148
|
|
|
* @param mixed $default |
|
149
|
|
|
* @return \Illuminate\Http\Request|string|array |
|
150
|
|
|
*/ |
|
151
|
|
|
function request($key = null, $default = null) |
|
152
|
|
|
{ |
|
153
|
|
|
if (is_null($key)) { |
|
154
|
|
|
return app('request'); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
if (is_array($key)) { |
|
158
|
|
|
return app('request')->only($key); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
return data_get(app('request')->all(), $key, $default); |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
if (! function_exists('active')) { |
|
166
|
|
|
/** |
|
167
|
|
|
* Returns string 'active' if the current request URI matches the given patterns. |
|
168
|
|
|
* |
|
169
|
|
|
* @return string |
|
170
|
|
|
*/ |
|
171
|
|
|
function active() |
|
172
|
|
|
{ |
|
173
|
|
|
return call_user_func_array([app('request'), 'is'], func_get_args()) ? 'active' : ''; |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|