-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathFreeagent.php
More file actions
258 lines (193 loc) · 7.22 KB
/
Freeagent.php
File metadata and controls
258 lines (193 loc) · 7.22 KB
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
<?php
/*
---
Freeagent-PHP:
A PHP toolkit for accessing Freeagent
version: 0.1.0
copyrights:
- [Ryan Mitchell](@ryanhmitchell)
license:
- [MIT License]
---
*/
class Freeagent {
// credentials
private $clientId;
private $clientSecret;
// oauth urls
private $oauthAuthoriseURL = 'https://api.freeagent.com/v2/approve_app';
private $oauthAccessTokenURL = 'https://api.freeagent.com/v2/token_endpoint';
private $accessToken;
// base url for api calls
private $apiUrl = 'https://api.freeagent.com/v2/';
// debug mode
private $debug = false;
// default constructor
function __construct($clientId, $clientSecret, $sandbox=false){
$this->clientId = $clientId;
$this->clientSecret = $clientSecret;
if ($sandbox==true){
$this->oauthAuthoriseURL = preg_replace('/api/', 'api.sandbox', $this->oauthAuthoriseURL);
$this->oauthAccessTokenURL = preg_replace('/api/', 'api.sandbox', $this->oauthAccessTokenURL);
$this->apiUrl = preg_replace('/api/', 'api.sandbox', $this->apiUrl);
}
}
// pass off to oauth authorise url
public function getAuthoriseURL($callback){
$authoriseURL = $this->oauthAuthoriseURL;
$authoriseURL .= '?response_type=code';
$authoriseURL .= '&client_id='.$this->clientId;
$authoriseURL .= '&redirect_uri='.urlencode($callback);
return $authoriseURL;
}
// convert an oauth code to an access token
public function getAccessToken($code, $callback){
// params to send to oauth receiver
$params = array(
'code' => $code,
'grant_type' => 'authorization_code',
'redirect_uri' => $callback
);
// call oauth
$result = $this->call('', 'oauth', $params);
// Save accessToken
$this->accessToken = $result->access_token;
// Return the response as an array
return $result;
}
// refresh token - they expire after 7 days
public function refreshToken($refreshToken){
// params to send to oauth receiver
$params = array(
'refresh_token' => $refreshToken,
'grant_type' => 'refresh_token',
);
// call oauth
$result = $this->call('', 'oauth', $params);
// Save accessToken
$this->accessToken = $result->access_token;
// Return the response as an array
return $result;
}
public function setAccessToken($accessToken){
$this->accessToken = $accessToken;
}
public function get($endpoint, $data=array()){
return $this->call($endpoint, 'get', $data);
}
public function post($endpoint, $data){
return $this->call($endpoint, 'post', $data);
}
public function put($endpoint, $data){
return $this->call($endpoint, 'put', $data);
}
public function delete($endpoint, $data){
return $this->call($endpoint, 'delete', $data);
}
/**************************************************************************
* Private functions
**************************************************************************/
private function parseHeaders($header){
$retVal = array();
$fields = explode("\r\n", preg_replace('/\x0D\x0A[\x09\x20]+/', ' ', $header));
foreach ($fields as $field){
if (preg_match('/([^:]+): (.+)/m', $field, $match)){
$match[1] = preg_replace_callback('/(?<=^|[\x09\x20\x2D])./',
create_function ('$matches', 'return strtoupper("\0");'), strtolower(trim($match[1])));
if (isset($retVal[$match[1]])){
$retVal[$match[1]] = array($retVal[$match[1]], $match[2]);
} else {
$retVal[$match[1]] = trim($match[2]);
}
}
}
return $retVal;
}
private function call($endpoint, $type, $data=array()){
$ch = curl_init();
// Setup curl options
$curl_options = array(
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 60,
CURLOPT_USERAGENT => 'Depot-PHP'
);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CAINFO, getcwd() . "/../cacert.pem");
$splitBody = false;
// Set curl url to call
if ($type == 'oauth'){
$curlURL = $this->oauthAccessTokenURL;
} else {
$curlURL = $this->apiUrl.$endpoint;
$curl_options += array(
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer '.$this->accessToken,
'Accept: application/json',
'Content-Type: application/json',
),
CURLOPT_HEADER => 1
);
$splitBody = true;
}
// type of request determines our headers
switch($type){
case 'post':
$curl_options = $curl_options + array(
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => json_encode($data)
);
break;
case 'put':
$curl_options = $curl_options + array(
CURLOPT_CUSTOMREQUEST => 'PUT',
CURLOPT_POSTFIELDS => json_encode($data)
);
break;
case 'delete':
$curl_options = $curl_options + array(
CURLOPT_CUSTOMREQUEST => 'DELETE',
CURLOPT_POSTFIELDS => $data
);
break;
case 'get':
$curlURL .= '?&'.http_build_query($data);
$curl_options = $curl_options + array(
);
break;
case 'oauth':
$curl_options = $curl_options + array(
CURLOPT_USERPWD => $this->clientId.':'.$this->clientSecret,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $data
);
break;
}
// add url
$curl_options = $curl_options + array(
CURLOPT_URL => $curlURL
);
// Set curl options
curl_setopt_array($ch, $curl_options);
// Send the request
$this->result = curl_exec($ch);
// freeagent returns location: for some responses
if ($splitBody){
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($this->result, 0, $header_size);
$this->result = substr($this->result, $header_size);
$this->headers = $this->parseHeaders($header);
}
// curl info
$this->info = curl_getinfo($ch);
if ($this->debug){
var_dump($this->result);
var_dump($this->info);
}
// Close the connection
curl_close($ch);
return json_decode($this->result);
}
}
?>