-
Notifications
You must be signed in to change notification settings - Fork 55
/
CurlRequest.class.php
149 lines (124 loc) · 3.63 KB
/
CurlRequest.class.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
<?php
/* Example:
* $req = new CurlRequest('http://google.com/');
* $req->setParams(array(
* 'foo' => 'bar'
* 'encoding' => 'json'
* ));
*
* $results = $req->execute();
*
* $results array(
* headers => http headers
* body => http response
* http_code => http status code
* url => url called (http://google.com/?foo=bar&encoding=json)
* )
*/
if(!function_exists('curl_init')) {
throw new Exception('CurlRequest needs the CURL PHP extension.');
}
// Custom exceptions for phpunit test purposes
class InvalidURLException extends Exception {}
class HTTPException extends Exception {}
class CurlRequest {
private $conn = false;
private $urlFormatting = true;
private $encoding = true;
private $CURL_OPTS = array(
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 60,
CURLOPT_USERAGENT => 'php_curl',
CURLOPT_HTTPGET => true,
CURLOPT_POST => false,
CURLOPT_HEADER => true,
CURLOPT_FOLLOWLOCATION => true
);
protected $params = array();
protected $method = 'get';
protected $url;
public function __construct($url, $urlFormatting = true) {
$this->urlFormatting = $urlFormatting;
$this->setURL($url);
}
public function setURL($url) {
if(!filter_var($url, FILTER_VALIDATE_URL)) {
throw new InvalidURLException($url);
}
$this->url = $url;
}
public function getUrl() {
return $this->url;
}
public function setOpt($option, $value) {
$this->CURL_OPTS[$option] = $value;
}
// default method is GET
public function setMethod($method) {
if($method == 'post') {
$this->setOpt(CURLOPT_POST, true);
$this->setOpt(CURLOPT_HTTPGET, false);
} elseif($method == 'get') {
$this->setOpt(CURLOPT_POST, false);
$this->setOpt(CURLOPT_HTTPGET, true);
}
$this->method = $method;
}
public function setParams($params, $encoding = true) {
if(is_array($params)) {
$this->params = $params;
}
$this->encoding = $encoding;
}
public function verifySSL($verify) {
$this->setOpt(CURLOPT_SSL_VERIFYPEER, $verify);
$this->setOpt(CURLOPT_SSL_VERIFYHOST, $verify);
}
public function execute() {
if(!is_resource($this->conn)) {
$this->conn = curl_init();
}
if($this->method == 'post') {
// CURLOPT_POSTFIELDS can take an array, so do that
$this->setOpt(CURLOPT_POSTFIELDS, $this->params);
} else {
// use default HTTP encoding
if($this->encoding) {
$query_string = http_build_query($this->params, null, '&');
} else {
$query_string = '';
foreach($this->params as $k => $v) {
$query_string .= "{$k}={$v}&";
}
}
if(!$this->urlFormatting || substr($this->url, -1) === '?') {
$this->url .= $query_string;
} else {
$this->url .= "?{$query_string}";
}
}
$this->setOpt(CURLOPT_URL, $this->url);
curl_setopt_array($this->conn, $this->CURL_OPTS);
$result = curl_exec($this->conn);
$info = curl_getinfo($this->conn);
// curl failed to execute call
if($result === false) {
throw new Exception('Curl failed with "' . curl_error($this->conn) . '"');
// server side soft error, gcm services
// } elseif(strstr($result, 'ERROR')) {
// throw new Exception('Request to server failed with "' . $result . '"');
// throw exception on non 2xx status
} elseif($info['http_code'] < 200 || $info['http_code'] >= 300) {
throw new HTTPException('Request failed with HTTP Status ' . $info['http_code']);
}
$return = array(
'headers' => substr($result, 0, $info['header_size']),
'body' => substr($result, $info['header_size']),
'http_code' => $info['http_code'],
'url' => $info['url']
);
curl_close($this->conn);
return $return;
}
}