HttpUtils.php
4.3 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
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
<?php
namespace Weasy\External\Utils;
use think\Log;
use Weasy\External\Utils\Error\HttpError;
use Weasy\External\Utils\Error\InternalError;
use Weasy\External\Utils\Error\NetWorkError;
class HttpUtils
{
//
// public:
//
/**
* 制作网址
*
* @param $queryArgs
*
* @return string
*/
static public function MakeUrl($queryArgs)
{
$base = "https://qyapi.weixin.qq.com";
if (substr($queryArgs, 0, 1) === "/")
return $base . $queryArgs;
return $base . "/" . $queryArgs;
}
/**
* Array to Json
*
* @param $arr
*
* @return string
*/
static public function Array2Json($arr)
{
$parts = array();
$is_list = false;
$keys = array_keys($arr);
$max_length = count($arr) - 1;
if (count($keys) > 0 && ($keys [0] === 0) && ($keys [$max_length] === $max_length)) {
$is_list = true;
for ($i = 0; $i < count($keys); $i++) {
if ($i != $keys [$i]) {
$is_list = false;
break;
}
}
}
foreach ($arr as $key => $value) {
if (is_array($value)) {
if ($is_list)
$parts [] = self::array2Json($value);
else
$parts [] = '"' . $key . '":' . self::array2Json($value);
} else {
$str = '';
if (!$is_list)
$str = '"' . $key . '":';
if (!is_string($value) && is_numeric($value) && $value < 2000000000)
$str .= $value;
elseif ($value === false)
$str .= 'false';
elseif ($value === true)
$str .= 'true';
else
$str .= '"' . addcslashes($value, "\\\"\n\r\t/") . '"';
$parts[] = $str;
}
}
$json = implode(',', $parts);
if ($is_list)
return '[' . $json . ']';
return '{' . $json . '}';
}
/**
* http获取
*
* @param string $url
*
* @return bool|string
* @throws HttpError
* @throws NetWorkError
* @throws InternalError
*/
static public function httpGet($url)
{
self::__checkDeps();
$ch = curl_init();
self::__setSSLOpts($ch, $url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
return self::__exec($ch);
}
/**
* http post
*
* @param string $url
* @param string|array $postData
*
* @return bool|string
* @throws HttpError
* @throws NetWorkError
* @throws InternalError
*/
static public function httpPost($url, $postData)
{
self::__checkDeps();
$ch = curl_init();
self::__setSSLOpts($ch, $url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
return self::__exec($ch);
}
//
// private:
//
/**
* 设置SSL选项
*
* @param $ch
* @param $url
*/
static private function __setSSLOpts($ch, $url)
{
if (stripos($url, "https://") !== false) {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_SSLVERSION, 1);
}
}
/**
* 执行
*
* @param $ch
*
* @return bool|string
* @throws HttpError
* @throws NetWorkError
*/
static private function __exec($ch)
{
$output = curl_exec($ch);
$status = curl_getinfo($ch);
curl_close($ch);
if ($output === false) {
throw new NetWorkError("network error");
}
if (intval($status["http_code"]) != 200) {
throw new HttpError("unexpected http code " . intval($status["http_code"]));
}
return $output;
}
/**
* 检查Deps
*
* @throws InternalError
*/
static private function __checkDeps()
{
if (!function_exists("curl_init")) {
throw new InternalError("missing curl extend");
}
}
}