common.php
1.8 KB
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
<?php
if (!function_exists('http_request')) {
// curl请求
function http_request($url, $timeout = 30, $header = array())
{
if (!function_exists('curl_init')) {
throw new Exception('server not install curl');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
if (!empty($header)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
}
$data = curl_exec($ch);
list($header, $data) = explode("\r\n\r\n", $data);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code == 301 || $http_code == 302) {
$matches = array();
preg_match('/Location:(.*?)\n/', $header, $matches);
$url = trim(array_pop($matches));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
$data = curl_exec($ch);
}
if ($data == false) {
curl_close($ch);
}
@curl_close($ch);
return $data;
}
}
if (!function_exists('insert_openid_info')) {
/**
* 将授权用户信息保存起来
*/
function insert_openid_info($data)
{
$data['upt_time'] = time();
$res = \think\Db::name("openid_info")->where("openid", $data['openid'])->find();
// file_put_contents("ccc.txt", date("Y-m-d H:i:s") . json_encode($data) . PHP_EOL, FILE_APPEND);
if (!empty($res)) {
\think\Db::name("openid_info")->where("openid", $data['openid'])->update($data);
} else {
\think\Db::name("openid_info")->insertGetId($data);
}
return true;
}
}