-
Notifications
You must be signed in to change notification settings - Fork 16
/
WufooApiWrapperBase.php
191 lines (161 loc) · 5.37 KB
/
WufooApiWrapperBase.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
<?php
/**
* Encapsulates the utility functions not required by user.
*
* @package default
* @author Timothy S Sabat
*/
class WufooApiWrapperBase {
protected function getHelper($url, $type, $iterator, $index = 'Hash') {
$this->curl = new WufooCurl();
$response = $this->curl->getAuthenticated($url, $this->apiKey);
$response = json_decode($response);
$className = 'Wufoo'.$type;
foreach ($response->$iterator as $obj) {
$arr[$obj->$index] = new $className($obj);
}
return $arr;
}
protected function getFullUrl($url) {
return 'https://'.$this->subdomain.'.'.$this->domain.'/api/v3/'.$url.'.json';
}
}
class WufooCurl {
public function __construct() {
//TIMTODO: set timout
}
public function getAuthenticated($url, $apiKey) {
$this->curl = curl_init($url);
$this->setBasicCurlOptions();
curl_setopt($this->curl, CURLOPT_USERPWD, $apiKey.':footastical');
$response = curl_exec($this->curl);
$this->setResultCodes();
$this->checkForCurlErrors();
$this->checkForGetErrors($response);
curl_close($this->curl);
return $response;
}
public function postAuthenticated($postParams, $url, $apiKey) {
$this->curl = curl_init($url);
$this->setBasicCurlOptions();
curl_setopt($this->curl, CURLOPT_HTTPHEADER, array('Content-type: multipart/form-data', 'Expect:'));
curl_setopt($this->curl, CURLOPT_POST, true);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $postParams);
curl_setopt($this->curl, CURLOPT_USERPWD, $apiKey.':footastical');
$response = curl_exec($this->curl);
$this->setResultCodes();
$this->checkForCurlErrors();
$this->checkForPostErrors($response);
curl_close($this->curl);
return $response;
}
//http://stackoverflow.com/questions/2081894/handling-put-delete-arguments-in-php
public function putAuthenticated($postParams, $url, $apiKey) {
$this->curl = curl_init($url);
$this->setBasicCurlOptions();
curl_setopt($this->curl, CURLOPT_HTTPHEADER, array('Content-type: multipart/form-data', 'Expect:'));
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($this->curl, CURLOPT_POSTFIELDS, http_build_query($postParams));
curl_setopt($this->curl, CURLOPT_USERPWD, $apiKey.':footastical');
$response = curl_exec($this->curl);
$this->setResultCodes();
$this->checkForCurlErrors();
$this->checkForPutErrors($response);
curl_close($this->curl);
return $response;
}
//http://stackoverflow.com/questions/2081894/handling-put-delete-arguments-in-php
public function deleteAuthenticated($postParams, $url, $apiKey) {
$this->curl = curl_init($url);
$this->setBasicCurlOptions();
curl_setopt($this->curl, CURLOPT_HTTPHEADER, array('Content-type: multipart/form-data', 'Expect:'));
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
curl_setopt($this->curl, CURLOPT_POSTFIELDS, http_build_query($postParams));
curl_setopt($this->curl, CURLOPT_USERPWD, $apiKey.':footastical');
$response = curl_exec($this->curl);
$this->setResultCodes();
$this->checkForCurlErrors();
//GET and DELETE both expect 200 response for success
$this->checkForGetErrors($response);
curl_close($this->curl);
return $response;
}
public function setBasicCurlOptions() {
//http://bugs.php.net/bug.php?id=47030
curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($this->curl, CURLOPT_USERAGENT, 'Wufoo API Wrapper');
curl_setopt($this->curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
}
private function setResultCodes() {
$this->ResultStatus = curl_getinfo($this->curl);
}
private function checkForCurlErrors() {
if(curl_errno($this->curl)) {
throw new WufooException(curl_error($this->curl), curl_errno($this->curl));
}
}
private function checkForGetErrors($response) {
switch ($this->ResultStatus['http_code']) {
case 200:
//ignore, this is good.
break;
case 401:
throw new WufooException('(401) Forbidden. Check your API key.', 401);
break;
default:
$this->throwResponseError($response);
break;
}
}
private function checkForPutErrors($response) {
switch ($this->ResultStatus['http_code']) {
case 201:
//ignore, this is good.
break;
case 401:
throw new WufooException('(401) Forbidden. Check your API key.', 401);
break;
default:
$this->throwResponseError($response);
break;
}
}
private function checkForPostErrors($response) {
switch ($this->ResultStatus['http_code']) {
case 200:
case 201:
//ignore, this is good.
break;
case 401:
throw new WufooException('(401) Forbidden. Check your API key.', 401);
break;
default:
$this->throwResponseError($response);
break;
}
}
private function throwResponseError($response) {
if ($response) {
$obj = json_decode($response);
throw new WufooException('('.$obj->HTTPCode.') '.$obj->Text, $this->ResultStatus['HTTP_CODE']);
} else {
throw new WufooException('(500) This is embarrassing... We did not anticipate this error type. Please contact support here: [email protected]', 500);
}
return $response;
}
}
/**
* Thrown by WufooCurl calls.
*
* @package default
* @author Timothy S Sabat
*/
class WufooException extends Exception {
public function __construct($message, $code) {
parent::__construct($message, $code);
}
};
?>