forked from freezey/php-togglSDK
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToggl.php
More file actions
95 lines (92 loc) · 3.4 KB
/
Copy pathToggl.php
File metadata and controls
95 lines (92 loc) · 3.4 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
<?php
require_once('Classloader.php');
$classLoader = new Toggl_Classloader();
spl_autoload_register(array(&$classLoader, "loadClass"));
use \Exception;
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){
Debug::write( 'Request URL: ' . $url );
}
unset($params['url']);
$method = $params['method'];
if (self::$debug == true){
Debug::write( '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){
Debug::write( 'API Token: ' . self::$token );
}
if ($method == 'POST'){
curl_setopt($curl, CURLOPT_POST, true);
$params = json_encode($params);
if (self::$debug == true){
Debug::write( "POST json: " . $params );
}
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($params),
));
}
if ($method == 'PUT'){
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
$params = json_encode($params);
if (self::$debug == true){
Debug::write( "PUT 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);
if (self::$debug == true){
Debug::write( "Curl result: $result" );
}
$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);
throw new \Exception($errorMessage, $info['http_code']);
}
}
public static function send($params = array()) {
return self::sendWithAuth($params);
}
public static function checkConnection(){
TogglUser::getCurrentUserData();
}
}
class Debug {
public static function write( $message )
{
echo $message;
}
}