/
CURLUtil.php
97 lines (88 loc) · 2.37 KB
/
CURLUtil.php
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
<?php
/**
* -------------------------
* curl common http request
* -------------------------
*
* Desc: curl common get http request、post request
*
* User: liangqi
* Date: 16/9/4
* Time: 下午7:06
*/
namespace WebUtil;
class CURLUtil
{
/**
* GET HTTP Request By Curl
*
* @param $url
* @param $header
*
* @return mixed
*/
public static function get($url, $header = []){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
self::makeHeader($curl, $header);
$r = curl_exec($curl);
curl_close($curl);
return $r;
}
/**
* 添加header
*
* @param $curl
* @param $header
*/
private static function makeHeader($curl, $header){
//HTTP请求头中"Accept-Encoding: "的值。支持的编码有"identity","deflate"和"gzip"。如果为空字符串"",请求头会发送所有支持的编码类型。
curl_setopt($curl,CURLOPT_ENCODING, '');
if ($header) {
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
}
}
/**
* post request by default format (x-www-form-urlencoded)
*
* @param $url
* @param $data
* @param $header
*
* @return mixed
*/
public static function post($url, $data, $header = []) {
$curl = curl_init();
self::makeHeader($curl, $header);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
$r = curl_exec($curl);
curl_close($curl);
return $r;
}
/**
* post by raw string
*
* @param $url
* @param $data
* @param array $header
* @return mixed
*/
public static function postByRaw($url, $data, $header = []){
$curl = curl_init();
self::makeHeader($curl, $header);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
if (!is_string($data)) {
$data = json_encode($data);
}
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$r = curl_exec($curl);
curl_close($curl);
return $r;
}
}