HTTPRequest PHP模拟HTTP POST/GET请求 支持HTTPS

0

HTTPRequest 是一个采用面向对象的概念开发的简化cURL和fsockopen的类。允许自定义头 参数,当cURL无法使用时自动调用fsockopen。

主文件代码 HTTPRequest.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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
<?php
 
class HTTPRequest {
	protected $hasCurl;
	protected $useCurl;
	protected $useBasicAuth;
	protected $HTTPVersion;
	protected $host;
	protected $port;
	protected $uri;
	protected $type;
	protected $query;
	protected $timeout;
	protected $error;
	protected $response;
	protected $responseText;
	protected $responseHeaders;
	protected $executed;
	protected $fsock;
	protected $curl;
	protected $additionalCurlOpts;
	protected $authUsername;
	protected $authPassword;
 
	public function __construct($host = null, $uri = '/', $port = 80, $useCurl = null, $timeout = 10) {
		if (!$host) {
			return false;
		}
		$this -> hasCurl = function_exists('curl_init');
		$this -> useCurl = $this -> hasCurl ? ($useCurl !== null ? $useCurl : true) : false;
		$this -> type = 'GET';
		$this -> HTTPVersion = '1.1';
		$this -> host = $host ? $host : $_SERVER['HTTP_HOST'];
		$this -> uri = $uri;
		$this -> port = $port;
		$this -> timeout = $timeout;
		$this -> setHeader('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8');
		$this -> setHeader('Accept-Language', 'en-us,en;q=0.5');
		$this -> setHeader('Accept-Encoding', 'deflate');	
		$this -> setHeader('Accept-Charset', 'ISO-8859-1,utf-8;q=0.7,*;q=0.7');
		$this -> setHeader('User-Agent', 'Mozilla/5.0 Firefox/3.6.12');
		$this -> setHeader('Connection', 'close');
		return $this;
	}
 
	public function setHost($host) {
		$this -> host = $host;
		return $this;
	}
 
	public function setRequestURI($uri) {
		$this -> uri = $uri;
		return $this;
	}
 
	public function setPort($port) {
		$this -> port = $port;
		return $this;
	}
 
	public function setTimeout($timeout) {
		$this -> timeout = $timeout;
		return $this;
	}
 
	// @TODO: Deprecate setGetData in preference of setQueryParams
	public function setGetData($get) {
		$this -> query = $get;
		return $this;
	}
 
	public function setQueryParams($get) {
		$this -> query = $get;
		return $this;
	}
 
	public function setUseCurl($use) {
		if ($use && $this -> hasCurl) {
			$this -> useCurl = true;
		} else {
			$this -> useCurl = false;
		}
		return $this;
	}
 
	public function setType($type) {
		if (in_array($type, array('POST', 'GET', 'PUT', 'DELETE'))) {
			$this -> type = $type;
		}
		return $this;
	}
 
	public function setData($data) {
		$this -> data = $data;
		return $this;
	}
 
	public function param($data) {
		$data_array = array();
		foreach ($data as $key => $val) {
			if (!is_string($val)) {
				$val = json_encode($val);
			}
			$data_array[] = urlencode($key).'='.urlencode($val);
		}
		return implode('&', $data_array);
	}
 
	public function setUrl($url) {
		$this -> url = $url;
		return $this;
	}
 
	public function setHeader($header, $content) {
		$this -> headers[$header] = $content;
		return $this;
	}
 
	public function setAdditionalCurlOpt($option, $value) {
		if (is_array($option)) {
			foreach ($option as $opt => $val) {
				$this -> setAdditionalCurlOpt($opt, $val);
			}
		} else {
			$this -> additionalCurlOpts[$option] = $value;
		}
	}
 
	public function setUseBasicAuth($set, $username = null, $password = null) {
		$this -> useBasicAuth = $set;
		if ($username) {
			$this -> setAuthUsername($username);
		}
		if ($password) {
			$this -> setAuthPassword($password);
		}
	}
 
	public function setAuthUsername($username = null) {
		$this -> authUsername = $username;
	}
 
	public function setAuthPassword($password = null) {
		$this -> authPassword = $password;
	}	
 
	public function execute() {
		if ($this -> useCurl) {
			$this -> curl_execute();
		} else {
			$this -> fsockget_execute();
		}
		return $this;
	}
 
	protected function curl_execute() {
		$uri = $this -> uri;
		$host = $this -> host;
		$type = $this -> type;
		$port = $this -> port;
		$data = property_exists($this, 'data') ? HTTPRequest::param($this -> data) : false;
		$timeout = $this -> timeout;
 
		// Initiate cURL.
		$ch = curl_init();
 
		// Set request type.
		if ($type === 'GET') {
			curl_setopt($ch, CURLOPT_HTTPGET, true);
		} else if ($type === 'POST') {
			curl_setopt($ch, CURLOPT_POST, true);
			if ($data) {
				curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
			}
		} else if ($type === 'PUT') {
			curl_setopt($ch, CURLOPT_PUT, true);
		} else {
			curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $type);
		}
 
		// Grab query string.
		$query = property_exists($this, 'query') && $this -> query ? '?'.HTTPRequest::param($this -> query) : '';
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 
		// Set additional headers.
		$headers = array();
		foreach ($this -> headers as $name => $val) {
			$headers[] = $name.': '.$val;
		}
		curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
 
		// Do stuff it it's HTTPS/SSL.
		if ($port == 443) {
			$protocol = 'https';
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
			curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
		} else {
			$protocol = 'http';
		}
 
		if (!empty($this -> additonalCurlOpts)) {
			foreach ($this -> additionalCurlOpts as $option => $value) {
				curl_setopt($ch, $option, $value);
			}
		}
		// Build and set URL.
		$url = $protocol.'://'.$host.$uri.$query;
		curl_setopt($ch, CURLOPT_URL, $url); 
		curl_setopt($ch, CURLOPT_PORT, $port);
 
		// Add any authentication to the request.
		// Currently supports only HTTP Basic Auth.
		if ($this -> useBasicAuth === true) {
			curl_setopt($ch, CURLOPT_USERPWD, $this -> authUsername.':'.$this -> authPassword);	
			curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
		}
 
		// Execute!
		$rsp = curl_exec($ch);
		$this -> curl = $ch;
		$this -> executed = true;
 
		// Handle an error.
		if (!$error = curl_error($ch)) {
			$this -> response = array('responseText' => $rsp) + curl_getinfo($ch);
			$this -> responseHeaders = curl_getinfo($ch);
			$this -> responseText = $rsp;
		} else {
			$this -> error = $error;
		}
	}
 
	protected function fsockget_execute() {
		$uri = $this -> uri;
		$host = $this -> host;
		$port = $this -> port;
		$type = $this -> type;
		$HTTPVersion = $this -> HTTPVersion;
		$data = property_exists($this, 'data') ? $this -> data : null;
		$crlf = "\r\n";
 
		$rsp = '';
 
		// Deal with the data first.
		if ($data && $type === 'POST') {
			$data = $this -> param($data);
		} else if ($data && $type === 'GET') {
			$get_data = $data;
			$data = $crlf;
		} else {
			$data = $crlf;
		}
		// Then add
		if ($type === 'POST') {
			$this -> setHeader('Content-Type', 'application/x-www-form-urlencoded');
			$this -> setHeader('Content-Length', strlen($data));
			$get_data = property_exists($this, 'query') && $this -> query ? HTTPRequest::param($this -> query) : false;
		} else {
			$this -> setHeader('Content-Type', 'text/plain');
			$this -> setHeader('Content-Length', strlen($crlf));
		}
		if ($type === 'GET') {
			if (isset($get_data)) {
				$get_data = $data;
			} else if ($this -> query) {
				$get_data = HTTPRequest::param($this -> query);
			}
		}
		if ($this -> useBasicAuth === true) {
			$this -> setHeader('Authorization', 'Basic '.base64_encode($this -> authUsername.':'.$this -> authPassword));
		}
		$headers = $this -> headers;
		$req = '';
		$req .= $type.' '.$uri.(isset($get_data) ? '?'.$get_data : '').' HTTP/'.$HTTPVersion.$crlf;
		$req .= "Host: ".$host.$crlf;
		foreach ($headers as $header => $content) {
			$req .= $header.': '.$content.$crlf;
		}
		$req .= $crlf;
		if ($type === 'POST') {
			$req .= $data;
		} else {
			$req .= $crlf;
		}
 
		// Construct hostname.
		$fsock_host = ($port == 443 ? 'ssl://' : '').$host;
 
		// Open socket.
		$httpreq = @fsockopen($fsock_host, $port, $errno, $errstr, 30);
 
		// Handle an error.
		if (!$httpreq) {
			$this -> error = $errno.': '.$errstr;
			return false;
		}
 
		// Send the request.
		fputs($httpreq, $req);
 
		// Receive the response.
		while ($line = fgets($httpreq)) {
			$rsp .= $line;
		}
 
 
		// Extract the headers and the responseText.
		list($headers, $responseText) = explode($crlf.$crlf, $rsp);
 
		// Store the finalized response.
		$this -> response = $rsp;
		$this -> responseText = $responseText;
		$this -> status = array_shift($headers);
 
		// Store the response headers.
		$headers = explode($crlf, $headers);
		$this -> responseHeaders = array();
		foreach ($headers as $header) {
			list($key, $val) = explode(': ', $header);
			$this -> responseHeaders[$key] = $val;
		}
 
		// Mark as executed.
		$this -> executed = true;
 
		// Store the resource so we can close it later.
		$this -> fsock = $httpreq;
	}
 
	public function close() {
		if (!$this -> executed) {
			return false;
		}
		if ($this -> useCurl) {
			$this -> curl_close();
		} else {
			$this -> fsockget_close();
		}
	}
 
	protected function fsockget_close() {
		fclose($this -> fsock);
	}
 
	protected function curl_close() {
		curl_close($this -> curl);
	}
 
	public function getError() {
		return $this -> error;
	}
 
	public function getResponse() {
		if (!$this -> executed) {
			return false;
		}
		return $this -> response;
	}
 
	public function getResponseText() {
		if (!$this -> executed) {
			return false;
		}
		return $this -> responseText;
	}
 
	public function getAllResponseHeaders() {
		if (!$this -> executed) {
			return false;
		}
		return $this -> responseHeaders;
	}
 
	public function getResponseHeader($header) {
		if (!$this -> executed) {
			return false;
		}
		$headers = $this -> responseHeaders;
		if (array_key_exists($header, $headers)) {
			return $headers[$header];
		}
	}
 
	public static function curlHeaders() {
		return array(
			'User-Agent' => CURLOPT_USERAGENT,
		);
	}
}

调用方法

1
2
3
4
$http = new HTTPRequest('www.google.com');
$http -> execute();
echo $http -> getResponseText();
$http -> close();

github项目链接HTTPRequest

另外找到一个比较优秀的github项目easy-http,EasyHttp源于WordPress中的WP_Http类,去除了所有对WordPress其他函数的依赖,将其拆分到不同的文件中,并做了少量简化,支持自动选择curl/fsockopen/fopen方式。

发表评论

您的邮箱不会公开,当您的评论有新的回复时,会通过您填写的邮箱向您发送评论内容。 必填字段 *

为何看不到我发布的评论?

正在提交, 请稍候...