-
Notifications
You must be signed in to change notification settings - Fork 4
/
Toggl.php
80 lines (71 loc) · 2.57 KB
/
Toggl.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
<?php
require_once('Classloader.php');
$classLoader = new Toggl_Classloader();
spl_autoload_register(array(&$classLoader, "loadClass"));
class Toggl{
/*
* API URL parts
*/
private static $token;
public static $debug = false;
public static $verifyPeer = true;
public static function setKey($apiKey) {
self::$token = $apiKey;
}
public static function verifyPeer($bool){
self::$verifyPeer = $bool;
}
private static function sendWithAuth($params) {
$url = $params['url'];
if (self::$debug == true){
echo 'Request URL: ' . $url;
}
unset($params['url']);
$method = $params['method'];
if (self::$debug == true){
echo 'Request method: ' . $method;
}
unset($params['method']);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_USERPWD, self::$token . ':api_token');
if (self::$verifyPeer == false){
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
}
if (self::$debug == true){
echo 'API Token: ' . self::$token;
}
if ($method == 'POST'){
curl_setopt($curl, CURLOPT_POST, true);
$params = json_encode($params);
if (self::$debug == true){
echo "POST json: " . $params;
}
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($params),
));
}
$result = curl_exec($curl);
$info = curl_getinfo($curl);
curl_close($curl);
$resultJson = json_decode($result, true);
if (is_array($resultJson)){
if (count($resultJson) == 1 && isset($resultJson['data'])){
$resultJson = $resultJson['data'];
}
return $resultJson;
} else {
$errorMessage = 'Toggl API call failed -- Request URL: ' . $url . (is_string($params)? ' Request Data: ' . $params : null) . ' Response code: ' . $info['http_code'] . ' Raw response dump: ' . $result . ' serialized CURL info: ' . serialize($info);
CakeLog::write('error', $errorMessage);
throw new Exception($errorMessage);
}
}
public static function send($params = array()) {
return self::sendWithAuth($params);
}
public static function checkConnection(){
TogglUser::getCurrentUserData();
}
}