|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
require_once('Tiqr/API/Entity/APIResult.php'); |
|
4
|
|
|
|
|
5
|
|
|
class Tiqr_API_Client |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* The API Base URL |
|
9
|
|
|
* |
|
10
|
|
|
* @var string |
|
11
|
|
|
*/ |
|
12
|
|
|
protected $_apiBaseURL; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* The API consumer key |
|
16
|
|
|
* |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $_consumerKey; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Set API baseURL |
|
23
|
|
|
* |
|
24
|
|
|
* @param string $apiBaseURL The API end point |
|
25
|
|
|
*/ |
|
26
|
|
|
public function setBaseURL($apiBaseURL) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->_apiBaseURL = rtrim($apiBaseURL, '/') . '/'; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Set Consumer key |
|
33
|
|
|
* |
|
34
|
|
|
* @param string $consumerKey The consumer key |
|
35
|
|
|
*/ |
|
36
|
|
|
public function setConsumerKey($consumerKey) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->_consumerKey = $consumerKey; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Calls the API |
|
43
|
|
|
* |
|
44
|
|
|
* @param string $resource The resource URL |
|
45
|
|
|
* @param string $method The HTTP Method (GET, POST, PUT, DELETE) |
|
46
|
|
|
* @param array $data Data send with request as key => value pairs |
|
47
|
|
|
* |
|
48
|
|
|
* @return Object |
|
49
|
|
|
* |
|
50
|
|
|
* @throws Exception |
|
51
|
|
|
*/ |
|
52
|
|
|
public function call($resource, $method = "GET", $data = array()) |
|
53
|
|
|
{ |
|
54
|
|
|
$headers['X-OATHService-ConsumerKey'] = $this->_consumerKey; |
|
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
$result = $this->callAPI($resource, $method, $data, $headers); |
|
57
|
|
|
|
|
58
|
|
|
if (2 == substr($result->code, 0, 1)) { |
|
59
|
|
|
return $result; |
|
60
|
|
|
} elseif (is_object($result->body)) { |
|
61
|
|
|
throw new Exception($result->body->error, $result->code); |
|
62
|
|
|
|
|
63
|
|
|
} else { |
|
64
|
|
|
throw new Exception('', $result->code); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Calls the API endpoint |
|
70
|
|
|
* |
|
71
|
|
|
* @param string $resource The resource URL |
|
72
|
|
|
* @param string $method The HTTP Method (GET, POST, PUT, DELETE) |
|
73
|
|
|
* @param array $data Data send with request as key => value pairs |
|
74
|
|
|
* @param array $headers HTTP Headers send with request as key => value pairs |
|
75
|
|
|
* |
|
76
|
|
|
* @return Tiqr_API_Entity_APIResult |
|
77
|
|
|
*/ |
|
78
|
|
|
protected function callAPI($resource, $method = "GET", $data = array(), $headers = array()) |
|
79
|
|
|
{ |
|
80
|
|
|
$ch = curl_init(); |
|
81
|
|
|
curl_setopt($ch, CURLOPT_URL, $this->_apiBaseURL . ltrim($resource, '/')); |
|
82
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
|
83
|
|
|
|
|
84
|
|
|
// Explicitly empty null values, because http_build_query will throw away |
|
85
|
|
|
// all null values, which would not let us make fields empty. |
|
86
|
|
|
if (is_array($data)) { |
|
|
|
|
|
|
87
|
|
|
foreach ($data as $key => $value) { |
|
88
|
|
|
if ($value == null) { |
|
89
|
|
|
$data[$key] = ''; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
switch ($method) { |
|
95
|
|
|
case 'POST': |
|
96
|
|
|
curl_setopt($ch, CURLOPT_POST, 1); |
|
97
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); |
|
98
|
|
|
break; |
|
99
|
|
|
case 'PUT': |
|
100
|
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); |
|
101
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); |
|
102
|
|
|
break; |
|
103
|
|
|
case 'DELETE': |
|
104
|
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); |
|
105
|
|
|
break; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
$headerArray = array(); |
|
109
|
|
|
foreach ($headers as $key => $value) { |
|
110
|
|
|
$headerArray[] = $key . ': ' . $value; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArray); |
|
114
|
|
|
|
|
115
|
|
|
$apiResult = curl_exec($ch); |
|
116
|
|
|
$resultCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
|
117
|
|
|
curl_close($ch); |
|
118
|
|
|
|
|
119
|
|
|
$result = new Tiqr_API_Entity_APIResult(); |
|
120
|
|
|
$result->code = $resultCode; |
|
121
|
|
|
$result->body = json_decode($apiResult); |
|
|
|
|
|
|
122
|
|
|
$result->rawBody = $apiResult; |
|
123
|
|
|
|
|
124
|
|
|
return $result; |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|