1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
|
<?php
// +----------------------------------------------------------------------
// | wechat-php-sdk
// +----------------------------------------------------------------------
// | 版权所有 2014~2017 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方文档: https://www.kancloud.cn/zoujingli/wechat-php-sdk
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | github开源项目:https://github.com/zoujingli/wechat-php-sdk
// +----------------------------------------------------------------------
namespace Wechat\Lib;
use CURLFile;
/**
* 微信接口通用类
*
* @category WechatSDK
* @subpackage library
* @author Anyon <zoujingli@qq.com>
* @date 2016/05/28 11:55
*/
class Tools
{
/**
* 判断字符串是否经过编码方法
* @param string $str
* @return bool
*/
static public function isBase64($str)
{
if ($str == base64_encode(base64_decode($str))) {
return true;
} else {
return false;
}
}
/**
* 产生随机字符串
* @param int $length 指定字符长度
* @param string $str 字符串前缀
* @return string
*/
static public function createNoncestr($length = 32, $str = "")
{
$chars = "abcdefghijklmnopqrstuvwxyz0123456789";
for ($i = 0; $i < $length; $i++) {
$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
}
return $str;
}
/**
* 数据生成签名
* @param array $data 签名数组
* @param string $method 签名方法
* @return bool|string 签名值
*/
static public function getSignature($data, $method = "sha1")
{
if (!function_exists($method)) {
return false;
}
ksort($data);
$params = array();
foreach ($data as $key => $value) {
$params[] = "{$key}={$value}";
}
return $method(join('&', $params));
}
/**
* 生成支付签名
* @param array $option
* @param string $partnerKey
* @return string
*/
static public function getPaySign($option, $partnerKey)
{
ksort($option);
$buff = '';
foreach ($option as $k => $v) {
$buff .= "{$k}={$v}&";
}
return strtoupper(md5("{$buff}key={$partnerKey}"));
}
/**
* XML编码
* @param mixed $data 数据
* @param string $root 根节点名
* @param string $item 数字索引的子节点名
* @param string $id 数字索引子节点key转换的属性名
* @return string
*/
static public function arr2xml($data, $root = 'xml', $item = 'item', $id = 'id')
{
return "<{$root}>" . self::_data_to_xml($data, $item, $id) . "</{$root}>";
}
/**
* XML内容生成
* @param array $data 数据
* @param string $item 子节点
* @param string $id 节点ID
* @param string $content 节点内容
* @return string
*/
static private function _data_to_xml($data, $item = 'item', $id = 'id', $content = '')
{
foreach ($data as $key => $val) {
is_numeric($key) && $key = "{$item} {$id}=\"{$key}\"";
$content .= "<{$key}>";
if (is_array($val) || is_object($val)) {
$content .= self::_data_to_xml($val);
} elseif (is_numeric($val)) {
$content .= $val;
} else {
$content .= '<![CDATA[' . preg_replace("/[\\x00-\\x08\\x0b-\\x0c\\x0e-\\x1f]/", '', $val) . ']]>';
}
list($_key, ) = explode(' ', $key . ' ');
$content .= "</$_key>";
}
return $content;
}
/**
* 将xml转为array
* @param string $xml
* @return array
*/
static public function xml2arr($xml)
{
$disableEntities = libxml_disable_entity_loader(true);
$result = json_decode(Tools::json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
libxml_disable_entity_loader($disableEntities);
return $result;
}
/**
* 生成安全JSON数据
* @param array $array
* @return string
*/
static public function json_encode($array)
{
return preg_replace_callback('/\\\\u([0-9a-f]{4})/i', function ($matches) {
return mb_convert_encoding(pack("H*", $matches[1]), "UTF-8", "UCS-2BE");
}, json_encode($array));
}
/**
* 以get方式提交请求
* @param $url
* @return bool|mixed
*/
static public function httpGet($url)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSLVERSION, 1);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
list($content, $status) = array(curl_exec($curl), curl_getinfo($curl), curl_close($curl));
return (intval($status["http_code"]) === 200) ? $content : false;
}
/**
* 以post方式提交请求
* @param string $url
* @param array|string $data
* @return bool|mixed
*/
static public function httpPost($url, $data)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, self::_buildPost($data));
list($content, $status) = array(curl_exec($curl), curl_getinfo($curl), curl_close($curl));
return (intval($status["http_code"]) === 200) ? $content : false;
}
/**
* 使用证书,以post方式提交xml到对应的接口url
* @param string $url POST提交的内容
* @param array $data 请求的地址
* @param string $ssl_cer 证书Cer路径 | 证书内容
* @param string $ssl_key 证书Key路径 | 证书内容
* @param int $second 设置请求超时时间
* @return bool|mixed
*/
static public function httpsPost($url, $data, $ssl_cer = null, $ssl_key = null, $second = 30)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_TIMEOUT, $second);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
if (!is_null($ssl_cer) && file_exists($ssl_cer) && is_file($ssl_cer)) {
curl_setopt($curl, CURLOPT_SSLCERTTYPE, 'PEM');
curl_setopt($curl, CURLOPT_SSLCERT, $ssl_cer);
}
if (!is_null($ssl_key) && file_exists($ssl_key) && is_file($ssl_key)) {
curl_setopt($curl, CURLOPT_SSLKEYTYPE, 'PEM');
curl_setopt($curl, CURLOPT_SSLKEY, $ssl_key);
}
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, self::_buildPost($data));
list($content, $status) = array(curl_exec($curl), curl_getinfo($curl), curl_close($curl));
return (intval($status["http_code"]) === 200) ? $content : false;
}
/**
* POST数据过滤处理
* @param array $data
* @return array
*/
static private function _buildPost(&$data)
{
if (is_array($data)) {
foreach ($data as &$value) {
if (is_string($value) && $value[0] === '@' && class_exists('CURLFile', false)) {
$filename = realpath(trim($value, '@'));
file_exists($filename) && $value = new CURLFile($filename);
}
}
}
return $data;
}
/**
* 读取微信客户端IP
* @return null|string
*/
static public function getAddress()
{
foreach (array('HTTP_X_FORWARDED_FOR', 'HTTP_CLIENT_IP', 'HTTP_X_CLIENT_IP', 'HTTP_X_CLUSTER_CLIENT_IP', 'REMOTE_ADDR') as $header) {
if (!isset($_SERVER[$header]) || ($spoof = $_SERVER[$header]) === null) {
continue;
}
sscanf($spoof, '%[^,]', $spoof);
if (!filter_var($spoof, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
$spoof = null;
} else {
return $spoof;
}
}
return '0.0.0.0';
}
/**
* 设置缓存,按需重载
* @param string $cachename
* @param mixed $value
* @param int $expired
* @return bool
*/
static public function setCache($cachename, $value, $expired = 0)
{
return Cache::set($cachename, $value, $expired);
}
/**
* 获取缓存,按需重载
* @param string $cachename
* @return mixed
*/
static public function getCache($cachename)
{
return Cache::get($cachename);
}
/**
* 清除缓存,按需重载
* @param string $cachename
* @return bool
*/
static public function removeCache($cachename)
{
return Cache::del($cachename);
}
/**
* SDK日志处理方法
* @param string $msg 日志行内容
* @param string $type 日志级别
*/
static public function log($msg, $type = 'MSG')
{
Cache::put($type . ' - ' . $msg);
}
}
|