|
@@ -35,6 +35,19 @@ class HttpHelper |
|
@@ -35,6 +35,19 @@ class HttpHelper |
35
|
* @param array $options 扩展参数
|
35
|
* @param array $options 扩展参数
|
36
|
* @return mixed|string
|
36
|
* @return mixed|string
|
37
|
*/
|
37
|
*/
|
|
|
38
|
+ public static function postheader($url, $params = [], $options = [],$header)
|
|
|
39
|
+ {
|
|
|
40
|
+ $req = self::sendRequestheader($url, $params, 'POST', $options,$header);
|
|
|
41
|
+ return $req['ret'] ? $req['msg'] : '';
|
|
|
42
|
+ }
|
|
|
43
|
+
|
|
|
44
|
+ /**
|
|
|
45
|
+ * 发送一个POST请求
|
|
|
46
|
+ * @param string $url 请求URL
|
|
|
47
|
+ * @param array $params 请求参数
|
|
|
48
|
+ * @param array $options 扩展参数
|
|
|
49
|
+ * @return mixed|string
|
|
|
50
|
+ */
|
38
|
public static function get($url, $params = [], $options = [])
|
51
|
public static function get($url, $params = [], $options = [])
|
39
|
{
|
52
|
{
|
40
|
$req = self::sendRequest($url, $params, 'GET', $options);
|
53
|
$req = self::sendRequest($url, $params, 'GET', $options);
|
|
@@ -75,7 +88,7 @@ class HttpHelper |
|
@@ -75,7 +88,7 @@ class HttpHelper |
75
|
* @param mixed $options CURL的参数
|
88
|
* @param mixed $options CURL的参数
|
76
|
* @return array
|
89
|
* @return array
|
77
|
*/
|
90
|
*/
|
78
|
- public static function sendRequest($url, $params = [], $method = 'POST', $options = [])
|
91
|
+ public static function sendRequestheader($url, $params = [], $method = 'POST', $options = [],$header)
|
79
|
{
|
92
|
{
|
80
|
$method = strtoupper($method);
|
93
|
$method = strtoupper($method);
|
81
|
$protocol = substr($url, 0, 5);
|
94
|
$protocol = substr($url, 0, 5);
|
|
@@ -104,7 +117,7 @@ class HttpHelper |
|
@@ -104,7 +117,7 @@ class HttpHelper |
104
|
$defaults[CURLOPT_TIMEOUT] =7;
|
117
|
$defaults[CURLOPT_TIMEOUT] =7;
|
105
|
|
118
|
|
106
|
// disable 100-continue
|
119
|
// disable 100-continue
|
107
|
- curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
|
120
|
+ curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization:'.$header));
|
108
|
|
121
|
|
109
|
if ('https' == $protocol) {
|
122
|
if ('https' == $protocol) {
|
110
|
$defaults[CURLOPT_SSL_VERIFYPEER] = false;
|
123
|
$defaults[CURLOPT_SSL_VERIFYPEER] = false;
|
|
@@ -134,6 +147,7 @@ class HttpHelper |
|
@@ -134,6 +147,7 @@ class HttpHelper |
134
|
];
|
147
|
];
|
135
|
}
|
148
|
}
|
136
|
|
149
|
|
|
|
150
|
+
|
137
|
/**
|
151
|
/**
|
138
|
* 异步发送一个请求
|
152
|
* 异步发送一个请求
|
139
|
* @param string $url 请求的链接
|
153
|
* @param string $url 请求的链接
|
|
@@ -219,35 +233,35 @@ class HttpHelper |
|
@@ -219,35 +233,35 @@ class HttpHelper |
219
|
}
|
233
|
}
|
220
|
|
234
|
|
221
|
public function curl($url,$method,$params=[],$auth=[]){
|
235
|
public function curl($url,$method,$params=[],$auth=[]){
|
222
|
- //初始化CURL句柄
|
236
|
+ //初始化CURL句柄
|
223
|
$curl = curl_init();
|
237
|
$curl = curl_init();
|
224
|
|
238
|
|
225
|
curl_setopt($curl, CURLOPT_URL, $url);//设置请求的URL
|
239
|
curl_setopt($curl, CURLOPT_URL, $url);//设置请求的URL
|
226
|
- #curl_setopt($curl, CURLOPT_HEADER, false);// 不要http header 加快效率
|
240
|
+ #curl_setopt($curl, CURLOPT_HEADER, false);// 不要http header 加快效率
|
227
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER,1); //设为TRUE把curl_exec()结果转化为字串,而不是直接输出
|
241
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER,1); //设为TRUE把curl_exec()结果转化为字串,而不是直接输出
|
228
|
|
242
|
|
229
|
- //SSL验证
|
243
|
+ //SSL验证
|
230
|
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求时要设置为false 不验证证书和hosts FALSE 禁止 cURL 验证对等证书(peer's certificate), 自cURL 7.10开始默认为 TRUE。从 cURL 7.10开始默认绑定安装。
|
244
|
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求时要设置为false 不验证证书和hosts FALSE 禁止 cURL 验证对等证书(peer's certificate), 自cURL 7.10开始默认为 TRUE。从 cURL 7.10开始默认绑定安装。
|
231
|
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);//检查服务器SSL证书中是否存在一个公用名(common name)。
|
245
|
+ curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);//检查服务器SSL证书中是否存在一个公用名(common name)。
|
232
|
|
246
|
|
233
|
$header[] = "Content-Type:application/json;charset=utf-8";
|
247
|
$header[] = "Content-Type:application/json;charset=utf-8";
|
234
|
- if(!empty($header)){
|
|
|
235
|
- curl_setopt ( $curl, CURLOPT_HTTPHEADER, $header );//设置 HTTP 头字段的数组。格式: array('Content-type: text/plain', 'Content-length: 100')
|
|
|
236
|
- }
|
248
|
+ if(!empty($header)){
|
|
|
249
|
+ curl_setopt ( $curl, CURLOPT_HTTPHEADER, $header );//设置 HTTP 头字段的数组。格式: array('Content-type: text/plain', 'Content-length: 100')
|
|
|
250
|
+ }
|
237
|
|
251
|
|
238
|
- //请求时间
|
252
|
+ //请求时间
|
239
|
$timeout = 30;
|
253
|
$timeout = 30;
|
240
|
- curl_setopt ($curl, CURLOPT_CONNECTTIMEOUT, $timeout);//设置连接等待时间
|
254
|
+ curl_setopt ($curl, CURLOPT_CONNECTTIMEOUT, $timeout);//设置连接等待时间
|
241
|
|
255
|
|
242
|
- //不同请求方法的数据提交
|
256
|
+ //不同请求方法的数据提交
|
243
|
switch ($method){
|
257
|
switch ($method){
|
244
|
case "GET" :
|
258
|
case "GET" :
|
245
|
curl_setopt($curl, CURLOPT_HTTPGET, true);//TRUE 时会设置 HTTP 的 method 为 GET,由于默认是 GET,所以只有 method 被修改时才需要这个选项。
|
259
|
curl_setopt($curl, CURLOPT_HTTPGET, true);//TRUE 时会设置 HTTP 的 method 为 GET,由于默认是 GET,所以只有 method 被修改时才需要这个选项。
|
246
|
break;
|
260
|
break;
|
247
|
case "POST":
|
261
|
case "POST":
|
248
|
if(is_array($params)){
|
262
|
if(is_array($params)){
|
249
|
- $params = json_encode($params,320);
|
|
|
250
|
- }
|
263
|
+ $params = json_encode($params,320);
|
|
|
264
|
+ }
|
251
|
#curl_setopt($curl, CURLOPT_POST,true);//TRUE 时会发送 POST 请求,类型为:application/x-www-form-urlencoded,是 HTML 表单提交时最常见的一种。
|
265
|
#curl_setopt($curl, CURLOPT_POST,true);//TRUE 时会发送 POST 请求,类型为:application/x-www-form-urlencoded,是 HTML 表单提交时最常见的一种。
|
252
|
#curl_setopt($curl, CURLOPT_NOBODY, true);//TRUE 时将不输出 BODY 部分。同时 Mehtod 变成了 HEAD。修改为 FALSE 时不会变成 GET。
|
266
|
#curl_setopt($curl, CURLOPT_NOBODY, true);//TRUE 时将不输出 BODY 部分。同时 Mehtod 变成了 HEAD。修改为 FALSE 时不会变成 GET。
|
253
|
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");//HTTP 请求时,使用自定义的 Method 来代替"GET"或"HEAD"。对 "DELETE" 或者其他更隐蔽的 HTTP 请求有用。 有效值如 "GET","POST","CONNECT"等等;
|
267
|
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");//HTTP 请求时,使用自定义的 Method 来代替"GET"或"HEAD"。对 "DELETE" 或者其他更隐蔽的 HTTP 请求有用。 有效值如 "GET","POST","CONNECT"等等;
|
|
@@ -264,18 +278,77 @@ class HttpHelper |
|
@@ -264,18 +278,77 @@ class HttpHelper |
264
|
break;
|
278
|
break;
|
265
|
}
|
279
|
}
|
266
|
|
280
|
|
267
|
- //传递一个连接中需要的用户名和密码,格式为:"[username]:[password]"。
|
281
|
+ //传递一个连接中需要的用户名和密码,格式为:"[username]:[password]"。
|
268
|
if (!empty($auth) && isset($auth['username']) && isset($auth['password'])) {
|
282
|
if (!empty($auth) && isset($auth['username']) && isset($auth['password'])) {
|
269
|
- curl_setopt($curl, CURLOPT_USERPWD, "{$auth['username']}:{$auth['password']}");
|
|
|
270
|
- }
|
283
|
+ curl_setopt($curl, CURLOPT_USERPWD, "{$auth['username']}:{$auth['password']}");
|
|
|
284
|
+ }
|
271
|
|
285
|
|
272
|
|
286
|
|
273
|
- $data = curl_exec($curl);//执行预定义的CURL
|
|
|
274
|
- $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);//获取http返回值,最后一个收到的HTTP代码
|
287
|
+ $data = curl_exec($curl);//执行预定义的CURL
|
|
|
288
|
+ $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);//获取http返回值,最后一个收到的HTTP代码
|
275
|
curl_close($curl);//关闭cURL会话
|
289
|
curl_close($curl);//关闭cURL会话
|
276
|
// $res = json_decode($data,true);
|
290
|
// $res = json_decode($data,true);
|
277
|
|
291
|
|
278
|
return $data;
|
292
|
return $data;
|
279
|
- }
|
293
|
+ }
|
|
|
294
|
+
|
|
|
295
|
+ public function curlheader($url,$method,$params=[],$auth=[],$headers){
|
|
|
296
|
+ //初始化CURL句柄
|
|
|
297
|
+ $curl = curl_init();
|
|
|
298
|
+ curl_setopt($curl, CURLOPT_URL, $url);//设置请求的URL
|
|
|
299
|
+ curl_setopt($curl, CURLOPT_HEADER, false);// 不要http header 加快效率
|
|
|
300
|
+ curl_setopt($curl, CURLOPT_RETURNTRANSFER,1); //设为TRUE把curl_exec()结果转化为字串,而不是直接输出
|
|
|
301
|
+
|
|
|
302
|
+ //SSL验证
|
|
|
303
|
+ curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求时要设置为false 不验证证书和hosts FALSE 禁止 cURL 验证对等证书(peer's certificate), 自cURL 7.10开始默认为 TRUE。从 cURL 7.10开始默认绑定安装。
|
|
|
304
|
+ curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);//检查服务器SSL证书中是否存在一个公用名(common name)。
|
|
|
305
|
+
|
|
|
306
|
+ $header[] = "Content-Type:application/json;charset=utf-8;Authorization=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6IjE5OTY5MTA2NzEwIiwicGFzc3dvcmQiOiJhMGUxNWM0NDRmOGNiY2NiNTAwM2U1MDk5OGI2OGZhNyIsImV4cCI6MTczMTY2ODA5MSwiaXNzIjoidWltcyJ9.W3RBwrFdidL9ERykKC_gHtjp911xrkNvUKglSes1w7c";
|
|
|
307
|
+ if(!empty($header)){
|
|
|
308
|
+ curl_setopt ( $curl, CURLOPT_HTTPHEADER, $header );//设置 HTTP 头字段的数组。格式: array('Content-type: text/plain', 'Content-length: 100')
|
|
|
309
|
+ }
|
|
|
310
|
+
|
|
|
311
|
+ //请求时间
|
|
|
312
|
+ $timeout = 30;
|
|
|
313
|
+ curl_setopt ($curl, CURLOPT_CONNECTTIMEOUT, $timeout);//设置连接等待时间
|
|
|
314
|
+
|
|
|
315
|
+ //不同请求方法的数据提交
|
|
|
316
|
+ switch ($method){
|
|
|
317
|
+ case "GET" :
|
|
|
318
|
+ curl_setopt($curl, CURLOPT_HTTPGET, true);//TRUE 时会设置 HTTP 的 method 为 GET,由于默认是 GET,所以只有 method 被修改时才需要这个选项。
|
|
|
319
|
+ break;
|
|
|
320
|
+ case "POST":
|
|
|
321
|
+ if(is_array($params)){
|
|
|
322
|
+ $params = json_encode($params,320);
|
|
|
323
|
+ }
|
|
|
324
|
+ #curl_setopt($curl, CURLOPT_POST,true);//TRUE 时会发送 POST 请求,类型为:application/x-www-form-urlencoded,是 HTML 表单提交时最常见的一种。
|
|
|
325
|
+ #curl_setopt($curl, CURLOPT_NOBODY, true);//TRUE 时将不输出 BODY 部分。同时 Mehtod 变成了 HEAD。修改为 FALSE 时不会变成 GET。
|
|
|
326
|
+ curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");//HTTP 请求时,使用自定义的 Method 来代替"GET"或"HEAD"。对 "DELETE" 或者其他更隐蔽的 HTTP 请求有用。 有效值如 "GET","POST","CONNECT"等等;
|
|
|
327
|
+ //设置提交的信息
|
|
|
328
|
+ curl_setopt($curl, CURLOPT_POSTFIELDS,$params);//全部数据使用HTTP协议中的 "POST" 操作来发送。
|
|
|
329
|
+ break;
|
|
|
330
|
+ case "PUT" :
|
|
|
331
|
+ curl_setopt ($curl, CURLOPT_CUSTOMREQUEST, "PUT");
|
|
|
332
|
+ curl_setopt($curl, CURLOPT_POSTFIELDS,json_encode($params,320));
|
|
|
333
|
+ break;
|
|
|
334
|
+ case "DELETE":
|
|
|
335
|
+ curl_setopt ($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
|
|
|
336
|
+ curl_setopt($curl, CURLOPT_POSTFIELDS,$params);
|
|
|
337
|
+ break;
|
|
|
338
|
+ }
|
|
|
339
|
+
|
|
|
340
|
+ //传递一个连接中需要的用户名和密码,格式为:"[username]:[password]"。
|
|
|
341
|
+ if (!empty($auth) && isset($auth['username']) && isset($auth['password'])) {
|
|
|
342
|
+ curl_setopt($curl, CURLOPT_USERPWD, "{$auth['username']}:{$auth['password']}");
|
|
|
343
|
+ }
|
|
|
344
|
+
|
|
|
345
|
+
|
|
|
346
|
+ $data = curl_exec($curl);//执行预定义的CURL
|
|
|
347
|
+ $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);//获取http返回值,最后一个收到的HTTP代码
|
|
|
348
|
+ curl_close($curl);//关闭cURL会话
|
|
|
349
|
+// $res = json_decode($data,true);
|
|
|
350
|
+
|
|
|
351
|
+ return $data;
|
|
|
352
|
+ }
|
280
|
|
353
|
|
281
|
} |
354
|
} |