-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStatHat.php
More file actions
199 lines (153 loc) · 4.4 KB
/
Copy pathStatHat.php
File metadata and controls
199 lines (153 loc) · 4.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
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
<?php
/**
* A PHP class that facilitates posting of stats to StatHat.com
* Based, in part, on StatHat's own stathat.php.
*
* Includes support for transactions.
*
* @author August Trometer (http://getyowza.com/contact)
*/
define('STATHAT_EZ_ENDPOINT','https://api.stathat.com/ez');
class StatHat {
public static $_ez_key;
public static $_transactions = array();
public static $_use_transaction = false;
/**
* Sets the EZ Key
*/
public static function setEZKey($key) {
self::$_ez_key = $key;
}
/**
* Starts the transaction
*
* This is only necessary if you want to send data
* in a single POST
*/
public static function beginTransaction() {
self::$_use_transaction = true;
}
/**
* Publishes a count stat
*
* This function adds the data to an array of transactions. If we're
* not using transactions, then the single item is instantly posted
* to the server
*/
public static function publishCount($stat_key, $count, $timestamp = NULL, $synchronous = false) {
$transaction = array(
'stat' => $stat_key,
'count' => $count,
);
// include timestamp information if necessary
if ($timestamp)
$transaction['t'] = $timestamp;
// add to the transaction array
self::$_transactions[] = $transaction;
// if we're not using transactions, go ahead and
// post the data
if (!self::$_use_transaction)
return self::commitTransaction($synchronous);
}
/**
* Publishes a count stat synchronously and returns the response
*
* This is a convenience function
*/
public static function publishCountSynchronous($stat_key, $count, $timestamp = NULL) {
return self::publishCount($stat_key, $count, $timestamp, true);
}
/**
* Publishes a value stat
*
* This function adds the data to an array of transactions. If we're
* not using transactions, then the single item is instantly posted
* to the server
*/
public static function publishValue($stat_key, $value, $timestamp = NULL, $synchronous = false) {
$transaction = array(
'stat' => $stat_key,
'value' => $value,
);
// include timestamp information if necessary
if ($timestamp)
$transaction['t'] = $timestamp;
self::$_transactions[] = $transaction;
if (!self::$_use_transaction)
return self::commitTransaction($synchronous);
}
/**
* Publishes a value stat synchronously and returns the response
*
* This is a convenience function
*/
public static function publishValueSynchronous($stat_key, $value, $timestamp = NULL) {
return self::publishValue($stat_key, $value, $timestamp, true);
}
/**
* Posts the transaction data to the server
*/
public static function commitTransaction($synchronous = false) {
$data = array(
'ezkey' => self::$_ez_key,
'data' => array()
);
// build the transaction array
foreach(self::$_transactions as $transaction) {
$data['data'][] = $transaction;
}
// reset the transaction
self::$_use_transaction = false;
self::$_transactions = array();
$json = json_encode($data);
// post synchronous or asynchrnously, as desired
if ($synchronous)
return self::postJSONSynchronous($json);
else
self::postJSON($json);
}
/**
* Posts the transaction synchronously
*
* This is a convenience function
*/
public static function commitTransactionSynchronous() {
return self::commitTransaction(true);
}
/**
* Posts the JSON data asynchronously to the server
*/
function postJSON($json) {
$parts = parse_url(STATHAT_EZ_ENDPOINT);
$fp = fsockopen($parts['host'], 80, $errno, $errstr, 30);
$out = "POST {$parts['path']} HTTP/1.1\r\n";
$out .= "Host: {$parts['host']}\r\n";
$out .= "Content-Type: application/json\r\n";
$out .= "Content-Length: " . strlen($json) . "\r\n";
$out .= "Connection: Close\r\n\r\n";
$out .= $json;
fwrite($fp, $out);
fclose($fp);
}
/**
* Posts the JSON data synchronously to the server
*/
function postJSONSynchronous($json) {
$url = STATHAT_EZ_ENDPOINT;
$params = array(
'http' => array(
'method' => 'POST',
'content' => $json,
'header' => array('Content-Type: application/json')
)
);
$context = stream_context_create($params);
$fp = @fopen($url, 'rb', false, $context);
if (!$fp)
throw new Exception("Problem with $url, $php_errormsg");
$response = @stream_get_contents($fp);
if ($response === false)
throw new Exception("Problem reading data from $url, $php_errormsg");
return $response;
}
} // clas StatHat