HttpRequest.php
6.9 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
187
188
189
190
191
192
193
194
<?php
namespace tinymeng\tools;
use tinymeng\tools\exception\StatusCode;
use tinymeng\tools\exception\TinymengException;
/**
* Class HttpRequest
* @package tinymeng\tools
* @Author: TinyMeng <666@majiameng.com>
* @Created: 2020/11/12
*/
class HttpRequest
{
/**
* $proxy
* 例: 127.0.0.1:8080
*
* 上传图片请求事例:
$data = [
'file' => new \CURLFile($_FILES['file']['tmp_name'],$_FILES['file']['type'],$_FILES['file']['name']),
];
\tinymeng\tools\HttpRequest::httpPost($url,$data)
*/
/**
* Description: POST 请求
* Author: JiaMeng <666@majiameng.com>
* @param string $url 请求链接
* @param array $param 请求参数
* @param array $httpHeaders 添加请求头
* @param string $proxy 代理ip
* @param int $http_code 相应正确的状态码
* @return mixed
* @throws \Exception
*/
static public function httpPost($url, $param = array(), $httpHeaders = array(),$proxy='', $http_code = null)
{
/** 参数检测,object或者array进行http_build_query */
if(!empty($param) && is_array($param)){
$flag = false;
foreach ($param as $value){
//判断参数是否是一个对象 或者 是一个数组
if(is_array($value) || (is_string($value) && is_object($value))){
$flag = true;
break;
}
}
if($flag == true){
$param = http_build_query($param);
}
}
$curl = curl_init();
/** 设置请求链接 */
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
/** 设置请求参数 */
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $param);
/** 设置请求headers */
if(empty($httpHeaders)){
$httpHeaders = array(
"user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36"
);
}
if (is_array($httpHeaders)){
curl_setopt($curl, CURLOPT_HTTPHEADER, $httpHeaders);
}
/** gzip压缩 */
curl_setopt($curl, CURLOPT_ACCEPT_ENCODING, "gzip,deflate");
/** 不验证https证书和hosts */
if (stripos($url, "https://") !== FALSE) {
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
}
/** http代理 */
if(!empty($proxy)){
$proxy = explode(':',$proxy);
curl_setopt($curl, CURLOPT_PROXYAUTH, CURLAUTH_BASIC); //代理认证模式
curl_setopt($curl, CURLOPT_PROXY, "$proxy[0]"); //代理服务器地址
curl_setopt($curl, CURLOPT_PROXYPORT,$proxy[1]); //代理服务器端口
}
/** 请求 */
$content = curl_exec($curl);
/** 获取请求信息 */
$info = curl_getinfo($curl);
/** 关闭请求资源 */
curl_close($curl);
if($http_code != null){
/** 验证网络请求状态 */
if (intval($info["http_code"]) === 0) {
throw new TinymengException(StatusCode::COMMON_TINYMENG_REQUEST_METHOD,
'[httpPost]: POST request was aborted ! Request url :' . $url . ' , post request data : ' . var_export($param,true)
);
}elseif(intval($info["http_code"]) != $http_code){
throw new TinymengException(StatusCode::COMMON_TINYMENG_REQUEST_METHOD,
'[httpPost]: POST request was aborted ! Request url :' . $url . ' , post request data : ' . var_export($param,true).' ,return code : '.$info["http_code"] .' ,return content : '.$content
);
} else {
return $content;
}
}else{
return $content;
}
}
/**
* Description: GET 请求
* Author: JiaMeng <666@majiameng.com>
* Updater:
* @param string $url 请求链接
* @param array $param 请求参数
* @param array $httpHeaders 添加请求头
* @param string $proxy
* @param int $http_code 相应正确的状态码
* @return mixed
* @throws \Exception
*/
static public function httpGet($url, $param = array(), $httpHeaders = array(),$proxy= '', $http_code = 200)
{
$curl = curl_init();
/** 设置请求参数 */
if (!empty($param)) {
$url = $url . '?' . http_build_query($param);
}
/** 设置请求链接 */
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
/** 不验证https证书和hosts */
if (stripos($url, "https://") !== FALSE) {
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
}
/** http代理 */
if(!empty($proxy)){
$proxy = explode(':',$proxy);
curl_setopt($curl, CURLOPT_PROXYAUTH, CURLAUTH_BASIC); //代理认证模式
curl_setopt($curl, CURLOPT_PROXY, "$proxy[0]"); //代理服务器地址
curl_setopt($curl, CURLOPT_PROXYPORT,$proxy[1]); //代理服务器端口
}
/** 设置请求headers */
if(empty($httpHeaders)){
$httpHeaders = array(
"user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36"
);
}
if (is_array($httpHeaders)){
curl_setopt($curl, CURLOPT_HTTPHEADER, $httpHeaders);
}
/** gzip压缩 */
curl_setopt($curl, CURLOPT_ACCEPT_ENCODING, "gzip,deflate");
/** 请求 */
$content = curl_exec($curl);
/** 获取请求信息 */
$info = curl_getinfo($curl);
/** 关闭请求资源 */
curl_close($curl);
/** 验证网络请求状态 */
if($http_code != null){
if (intval($info["http_code"]) === 0) {
throw new TinymengException(StatusCode::COMMON_TINYMENG_REQUEST_METHOD,
'[httpGet]: GET request was aborted ! Request url :' . $url
);
}elseif(intval($info["http_code"]) != $http_code){
throw new TinymengException(StatusCode::COMMON_TINYMENG_REQUEST_METHOD,
'[httpGet]: GET request was aborted ! Request url :' . $url .' ,return code : '.$info["http_code"] .' ,return content : '.$content
);
} else {
return $content;
}
}else{
return $content;
}
}
}