This repository has been archived by the owner on Jan 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HttpRequest.php
90 lines (73 loc) · 2.49 KB
/
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
<?php
/**
* HttpRequest
*
* @author Skitsanos
*/
//application/x-www-form-urlencoded
//application/json
//text/xml
class HttpRequest {
protected $c;
protected $url;
var $method = "GET";
var $preventCaching = true;
var $useSsl = true;
var $headers = array();
var $verbose = false;
var $uploadMode = false;
function __construct($url) {
if (!function_exists('curl_init')) {
echo "Function curl_init, used by HttpRequest does not exist.\n";
}
$this->url = $url;
$this->c = curl_init($this->url);
}
function send($data=null, $filepath=null) {
if (count($this->headers) > 0) {
curl_setopt($this->c, CURLOPT_HEADER, false);
curl_setopt($this->c, CURLOPT_HTTPHEADER, $this->headers);
}
curl_setopt($this->c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->c, CURLOPT_FOLLOWLOCATION, true);
if ($this->useSsl) {
curl_setopt($this->c, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($this->c, CURLOPT_SSL_VERIFYHOST, 0);
}
if ($this->preventCaching) {
curl_setopt($this->c, CURLOPT_FORBID_REUSE, true);
curl_setopt($this->c, CURLOPT_FRESH_CONNECT, true);
}
if ($this->uploadMode) {
//curl_setopt($this->c, CURLOPT_URL, $filepath);
//curl_setopt($this->c, CURLOPT_UPLOAD, true);
curl_setopt($this->c, CURLOPT_POST, true);
$fp = fopen($filepath, 'r');
curl_setopt($this->c, CURLOPT_INFILE, $fp);
curl_setopt($this->c, CURLOPT_INFILESIZE, filesize($filepath));
}
switch (strtoupper($this->method)) {
case 'POST':
curl_setopt($this->c, CURLOPT_POST, true);
if ($data != null)
curl_setopt($this->c, CURLOPT_POSTFIELDS, $data);
break;
case 'HEAD':
curl_setopt($this->c, CURLOPT_NOBODY, true);
break;
case 'DELETE':
curl_setopt($this->c, CURLOPT_CUSTOMREQUEST, "DELETE");
break;
case 'PUT':
curl_setopt($this->c, CURLOPT_CUSTOMREQUEST, "PUT");
if ($data != null)
curl_setopt($this->c, CURLOPT_POSTFIELDS, $data);
break;
}
curl_setopt($this->c, CURLOPT_VERBOSE, $this->verbose);
$output = curl_exec($this->c);
curl_close($this->c);
return $output;
}
}
?>