-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCurlWrapper.php
187 lines (164 loc) · 4.26 KB
/
CurlWrapper.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
<?php
/**
* cURL Wrapper
*/
class CurlWrapper
{
// cURL URL
private $url;
// cURL options
private $options;
// cURL handle
private $handle;
// cURL response code and their associated messages
private $curlResponseMessages = array(
'0' => 'Could not connect to server',
'201' => 'Document created',
'200' => 'Success',
'404' => 'Project or project iteration not found',
'403' => 'Update forbidden',
'401' => 'Unauthorized',
'500' => 'Internal server error');
// Boolean indicating verbose mode or not
private $verbose;
// Boolean indicating debug mode or not
private $debug;
// String describing the purpose of the cURL call
private $description;
/**
* Constructor
* @param string $url The cURL URL
* @param array $options The cURL options
* @param boolean $verbose True to enable verbose mode
* @param boolean $debug True to enable debug mode
* @param string $description Description of the cURL request (needed in verbose mode)
*/
public function __construct($url, $options,
$verbose = false,
$debug = false,
$description = '')
{
if ($url !== '')
{
$this->verbose = $verbose;
$this->debug= $debug;
$this->description = $description;
// Initialize the cURL handle with the crafted API URL
$this->handle = curl_init($url);
// Set cURL options
$this->options = $options;
curl_setopt_array($this->getHandle(), $this->getOptions());
// Debug mode
if ($debug) {
curl_setopt($this->getHandle(), CURLOPT_NOPROGRESS, false);
curl_setopt($this->getHandle(), CURLOPT_PROGRESSFUNCTION, array('CurlWrapper', 'progressCallback'));
}
}
}
/**
* Callback function for CURLOPT_PROGRESSFUNCTION
*
* @param int $download_size Total download size
* @param int $downloaded_size Current download size
* @param int $upload_size Total updoad size
* @param int $uploaded_size Current upload size
*/
function progressCallback($download_size, $downloaded_size, $upload_size, $uploaded_size)
{
if ($download_size == 0 && $upload_size == 0)
$progress = 0;
else if ($download_size != 0)
$progress = round($downloaded_size * 100 / $download_size);
else
$progress = round($uploaded_size * 100 / $upload_size);
$timestamp = time();
echo "Progress at $timestamp: $progress %\n";
}
/**
* Close the cURL if the handle is of valid format
*/
public function __destruct()
{
if(gettype($this->getHandle()) == 'resource')
curl_close($this->getHandle());
}
/**
* Execute the cURL call
* @return mixed cURL response if the cURL call was successful, False otherwise
*/
public function fetch()
{
// Execute the cURL call
$response = curl_exec($this->getHandle());
$finalResponse = $response === '' ? true : $response;
// Report the outcome to the user
$result = $this->reportOutcome();
if ($result)
{
return $finalResponse;
}
return $result;
}
/**
* Given the HTTP response code,
* Notify the user of the cURL outcome with the appropriate message
*
* @return boolean True if the cURL call was successful, False otherwise
*/
public function reportOutcome()
{
// Retrieve the cURL response
$curlResponse = curl_getinfo($this->getHandle(), CURLINFO_HTTP_CODE);
if ($this->debug)
{
$data = curl_getinfo($this->getHandle());
print_r($data);
}
// If verbose mode is enabled, notify appropriately
// with respect to the cURL response code
if ($this->verbose)
{
// Default message
$msg = 'Unknown HTTP response';
if (isset($this->curlResponseMessages[strval($curlResponse)]))
$msg = $this->curlResponseMessages[strval($curlResponse)];
echo $this->getDescription()
. ": $curlResponse-"
. $msg
. "\n";
}
return ($curlResponse === 200 || $curlResponse === 201);
}
/**
* Return the url
* @return string
*/
public function getUrl()
{
return $this->url;
}
/**
* Return the options array
* @return array
*/
public function getOptions()
{
return $this->options;
}
/**
* Return the cURL handle
* @return resource
*/
public function getHandle()
{
return $this->handle;
}
/**
* Return the cURL description text
* @return string
*/
public function getDescription() {
return $this->description;
}
}
?>