1 | <?php |
||||
2 | namespace tinymeng\tools; |
||||
3 | |||||
4 | use tinymeng\tools\exception\StatusCode; |
||||
5 | use tinymeng\tools\exception\TException; |
||||
6 | /** |
||||
7 | * Class HttpRequest |
||||
8 | * @package tinymeng\tools |
||||
9 | * @Author: TinyMeng <[email protected]> |
||||
10 | * @Created: 2020/11/12 |
||||
11 | */ |
||||
12 | class HttpRequest |
||||
13 | { |
||||
14 | /** |
||||
15 | * $proxy |
||||
16 | * 例: 127.0.0.1:8080 |
||||
17 | * |
||||
18 | * 上传图片请求事例: |
||||
19 | $data = [ |
||||
20 | 'file' => new \CURLFile($_FILES['file']['tmp_name'],$_FILES['file']['type'],$_FILES['file']['name']), |
||||
21 | ]; |
||||
22 | \tinymeng\tools\HttpRequest::httpPost($url,$data) |
||||
23 | */ |
||||
24 | |||||
25 | static $httpHeaders = [ |
||||
26 | "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" |
||||
27 | ]; |
||||
28 | |||||
29 | /** |
||||
30 | * Description: POST 请求 |
||||
31 | * Author: JiaMeng <[email protected]> |
||||
32 | * @param string $url 请求链接 |
||||
33 | * @param array $param 请求参数 |
||||
34 | * @param array $httpHeaders 添加请求头 |
||||
35 | * @param string $proxy 代理ip |
||||
36 | * @param int $http_code 相应正确的状态码 |
||||
37 | * @return bool|string |
||||
38 | * @throws \Exception |
||||
39 | */ |
||||
40 | static public function httpPost(string $url, array $param = array(), array $httpHeaders = array(), string $proxy='', int $http_code = 200) |
||||
41 | { |
||||
42 | /** 参数检测,object或者array进行http_build_query */ |
||||
43 | if(!empty($param) && is_array($param)){ |
||||
44 | $flag = false; |
||||
45 | foreach ($param as $value){ |
||||
46 | //判断参数是否是一个对象 或者 是一个数组 |
||||
47 | if(is_array($value) || (is_string($value) && is_object($value))){ |
||||
48 | $flag = true; |
||||
49 | break; |
||||
50 | } |
||||
51 | } |
||||
52 | if($flag){ |
||||
53 | $param = http_build_query($param); |
||||
54 | } |
||||
55 | } |
||||
56 | |||||
57 | $curl = curl_init(); |
||||
58 | |||||
59 | /** 设置请求链接 */ |
||||
60 | curl_setopt($curl, CURLOPT_URL, $url); |
||||
61 | curl_setopt($curl, CURLOPT_HEADER, 0); |
||||
62 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); |
||||
63 | |||||
64 | /** 设置请求参数 */ |
||||
65 | curl_setopt($curl, CURLOPT_POST, true); |
||||
66 | curl_setopt($curl, CURLOPT_POSTFIELDS, $param); |
||||
67 | |||||
68 | /** 设置请求headers */ |
||||
69 | if(empty($httpHeaders)) $httpHeaders = self::$httpHeaders; |
||||
70 | curl_setopt($curl, CURLOPT_HTTPHEADER, $httpHeaders); |
||||
71 | |||||
72 | /** gzip压缩 */ |
||||
73 | curl_setopt($curl, CURLOPT_ACCEPT_ENCODING, "gzip,deflate"); |
||||
74 | |||||
75 | /** 不验证https证书和hosts */ |
||||
76 | if (stripos($url, "https://") !== FALSE) { |
||||
77 | curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); |
||||
78 | curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); |
||||
79 | curl_setopt($curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1); |
||||
80 | } |
||||
81 | |||||
82 | /** http代理 */ |
||||
83 | if(!empty($proxy)){ |
||||
84 | $proxy = explode(':',$proxy); |
||||
85 | curl_setopt($curl, CURLOPT_PROXYAUTH, CURLAUTH_BASIC); //代理认证模式 |
||||
86 | curl_setopt($curl, CURLOPT_PROXY, "$proxy[0]"); //代理服务器地址 |
||||
87 | curl_setopt($curl, CURLOPT_PROXYPORT,$proxy[1]); //代理服务器端口 |
||||
88 | } |
||||
89 | |||||
90 | /** 请求 */ |
||||
91 | $content = curl_exec($curl); |
||||
92 | /** 获取请求信息 */ |
||||
93 | $info = curl_getinfo($curl); |
||||
94 | /** 关闭请求资源 */ |
||||
95 | curl_close($curl); |
||||
96 | |||||
97 | if($http_code !== 0){ |
||||
98 | /** 验证网络请求状态 */ |
||||
99 | if (intval($info["http_code"]) === 0) { |
||||
100 | throw new TException(StatusCode::COMMON_TINYMENG_REQUEST_METHOD, |
||||
101 | '[httpPost]: POST request was aborted ! Request url :' . $url . ' , post request data : ' . var_export($param,true) |
||||
102 | ); |
||||
103 | }elseif(intval($info["http_code"]) != $http_code){ |
||||
104 | throw new TException(StatusCode::COMMON_TINYMENG_REQUEST_METHOD, |
||||
105 | '[httpPost]: POST request was aborted ! Request url :' . $url . ' , post request data : ' . var_export($param,true).' ,return code : '.$info["http_code"] .' ,return content : '.$content |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
106 | ); |
||||
107 | } else { |
||||
108 | return $content; |
||||
109 | } |
||||
110 | }else{ |
||||
111 | return $content; |
||||
112 | } |
||||
113 | } |
||||
114 | |||||
115 | /** |
||||
116 | * Description: GET 请求 |
||||
117 | * Author: JiaMeng <[email protected]> |
||||
118 | * Updater: |
||||
119 | * @param string $url 请求链接 |
||||
120 | * @param array $param 请求参数 |
||||
121 | * @param array $httpHeaders 添加请求头 |
||||
122 | * @param string $proxy |
||||
123 | * @param int $http_code 相应正确的状态码 |
||||
124 | * @return bool|string |
||||
125 | * @throws \Exception |
||||
126 | */ |
||||
127 | static public function httpGet(string $url, array $param = array(), array $httpHeaders = array(), string $proxy= '', int $http_code = 200) |
||||
128 | { |
||||
129 | $curl = curl_init(); |
||||
130 | |||||
131 | /** 设置请求参数 */ |
||||
132 | if (!empty($param)) { |
||||
133 | $url = $url . '?' . http_build_query($param); |
||||
134 | } |
||||
135 | |||||
136 | /** 设置请求链接 */ |
||||
137 | curl_setopt($curl, CURLOPT_URL, $url); |
||||
138 | curl_setopt($curl, CURLOPT_HEADER, 0); |
||||
139 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); |
||||
140 | |||||
141 | /** 不验证https证书和hosts */ |
||||
142 | if (stripos($url, "https://") !== FALSE) { |
||||
143 | curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); |
||||
144 | curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); |
||||
145 | curl_setopt($curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1); |
||||
146 | } |
||||
147 | |||||
148 | /** http代理 */ |
||||
149 | if(!empty($proxy)){ |
||||
150 | $proxy = explode(':',$proxy); |
||||
151 | curl_setopt($curl, CURLOPT_PROXYAUTH, CURLAUTH_BASIC); //代理认证模式 |
||||
152 | curl_setopt($curl, CURLOPT_PROXY, "$proxy[0]"); //代理服务器地址 |
||||
153 | curl_setopt($curl, CURLOPT_PROXYPORT,$proxy[1]); //代理服务器端口 |
||||
154 | } |
||||
155 | |||||
156 | /** 设置请求headers */ |
||||
157 | if(empty($httpHeaders)) $httpHeaders = self::$httpHeaders; |
||||
158 | curl_setopt($curl, CURLOPT_HTTPHEADER, $httpHeaders); |
||||
159 | |||||
160 | /** gzip压缩 */ |
||||
161 | curl_setopt($curl, CURLOPT_ACCEPT_ENCODING, "gzip,deflate"); |
||||
162 | |||||
163 | /** 请求 */ |
||||
164 | $content = curl_exec($curl); |
||||
165 | /** 获取请求信息 */ |
||||
166 | $info = curl_getinfo($curl); |
||||
167 | /** 关闭请求资源 */ |
||||
168 | curl_close($curl); |
||||
169 | |||||
170 | /** 验证网络请求状态 */ |
||||
171 | if($http_code !== 0){ |
||||
172 | if (intval($info["http_code"]) === 0) { |
||||
173 | throw new TException(StatusCode::COMMON_TINYMENG_REQUEST_METHOD, |
||||
174 | '[httpGet]: GET request was aborted ! Request url :' . $url |
||||
175 | ); |
||||
176 | }elseif(intval($info["http_code"]) != $http_code){ |
||||
177 | throw new TException(StatusCode::COMMON_TINYMENG_REQUEST_METHOD, |
||||
178 | '[httpGet]: GET request was aborted ! Request url :' . $url .' ,return code : '.$info["http_code"] .' ,return content : '.$content |
||||
0 ignored issues
–
show
Are you sure
$content of type string|true can be used in concatenation ?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
179 | ); |
||||
180 | } else { |
||||
181 | return $content; |
||||
182 | } |
||||
183 | }else{ |
||||
184 | return $content; |
||||
185 | } |
||||
186 | } |
||||
187 | |||||
188 | } |
||||
189 |