Repository navigation
Expand file tree
/
Copy pathAptgraph.php
More file actions
232 lines (208 loc) · 5 KB
/
Copy pathAptgraph.php
File metadata and controls
232 lines (208 loc) · 5 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
<?php
/**
* Aptgraph api library
*
* @author Aptgraph team
* @package Aptgraph
* @category Library
* @copyright (c) 2011 Aptgraph team
* @version 1.0
* @link http://aptgraph.com/
* @since Available since Release 1.0
*/
class Aptgraph {
protected $url = 'http://aptgraph.cascade/api/v1/';
protected $payload = array();
protected $apikey;
protected $result;
protected $result_headers;
public $error_messages;
public $error_codes;
/**
* __construct
*
* @param string $key The apikey
* @access public
* @return void
*/
public function __construct($key = FALSE)
{
if ($key)
{
$this->apikey = $key;
}
}
/**
* Increment a statistic in a graph
*
* Usage:
* $aptgraph = new Aptgraph($your_api_key);
* $aptgraph
* ->increment(23) // For graphid '23'
* ->send();
*
* Returns 'this' to enable method chaining
*
* @param Integer The graph id
* @return Aptgraph This class (as instanced object)
*/
public function increment($graphid)
{
$item = array('section' => 'statistic', 'action' => 'increment',
'graphid' => $graphid);
// Add to the payload
$this->add_to_payload($item);
return $this;
}
/**
* Add a statistic to a graph
*
* Usage:
* $aptgraph = new Aptgraph($your_api_key);
* $aptgraph
* ->add(11,123)
* ->add(11,124,'2010-09-07 13:30:00') // Add a timestamp
* ->add(11,125)
* ->add(11,726)
* ->send();
*
* Returns 'this' to enable method chaining
*
* @param Integer The graph id
* @param Number The value to add to the graph
* @return Aptgraph This class (as instanced object)
*/
public function add($graphid, $value, $timestamp = NULL)
{
$item = array('section' => 'statistic', 'action' => 'add',
'graphid' => $graphid, 'value' => $value);
// Add the timestamp if its been passed in
if ($timestamp !== NULL)
{
$item['timestamp'] = $timestamp;
}
// Add to the payload
$this->add_to_payload($item);
return $this;
}
/**
* Adds the item (action to process) to the payload. If a apikey has been
* set it adds it in too.
*
* @param array $item Command to run
* @access private
* @return void
*/
private function add_to_payload(array $item)
{
// If the apikey has been set, then add it into the item.
if ($this->apikey !== NULL)
{
$item['apikey'] = $this->apikey;
}
$this->payload[] = $item;
}
/**
* Clear all commands from the payload.
*
* @access public
* @return void
*/
public function reset_payload()
{
$this->payload = array();
}
/**
* Sends the payload to the api
*
* @return bool The result of the request
*/
public function send()
{
if (empty($this->payload)) return;
// Reset results, errors and headers
$this->result_headers = array();
$this->error_messages = array();
$this->error_codes = array();
$this->result = '';
// Send the payload as JSON
if (($json_payload = json_encode($this->payload)) === NULL)
{
throw new Exception('There was a problem converting the payload to JSON');
return FALSE;
}
// Construct the headers
$headers = array(
'Content-type: application/json; charset="utf-8"',
'Content-Length: '.strlen($json_payload),
'Accept: application/json',
'Expect:'
);
// Send the payload to the api
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_payload);
curl_setopt($ch, CURLOPT_URL, $this->url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_FILETIME, TRUE);
// Execute and store the results
if (($this->result = curl_exec($ch)) === FALSE)
{
throw new Exception('There was a error requesting the Aptgraph api: '.curl_error($ch),1000);
}
// Save the headers and close curl
$this->result_headers = curl_getinfo($ch);
curl_close($ch);
echo '<h1>RESULT</h1><hr/>';
var_dump($this->result);
echo '<hr>';
die;
// Reset the payload for the next call
$this->reset_payload();
// TODO Process the result and give useful information to the user
$decoded_result = json_decode($this->result, TRUE);
// Check each item in the result for a status != 0
$status = TRUE;
foreach ($decoded_result as $item)
{
if ($item['status'] != 0)
{
$status = FALSE;
$this->error_messages [] = $item['errors'];
$this->error_codes[] = $item['status'];
}
}
// Returns TRUE if there were no errors
return $status;
}
/**
* Returns the result headers
*
* @return Array Result headers
*/
public function get_result_headers()
{
return $this->result_headers;
}
/**
* Returns the results
*
* @param bool $decode Send the result back decoded or not
* @access public
* @return mixed Returns either an array (default) or the raw result (as a JSON string)
*/
public function get_result($decode = TRUE)
{
if ($decode)
{
return json_decode($this->result);
}
else
{
return $this->result;
}
}
}