Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
StatHat Class
// An simplified version of StatHat's own PHP code, wrapped in a class, with a lower timeout threshold.
class StatHat {
private static function ez_key() {
return "Your_StatHat_Key_Here"
//return $_SERVER['STATHAT_KEY'];
// StatHat's original code is very much _not_ asynchronous.
// In the event of DNS issues, this will lock your app for TIMEOUT seconds.
// It is set to 2 seconds, if you have frequent problems with timeouts, up the timeout, or fix your DNS :)
private static function do_async_post_request($url, $params)
foreach ($params as $key => &$val) {
if (is_array($val)) $val = implode(',', $val);
$post_params[] = $key.'='.urlencode($val);
$post_string = implode('&', $post_params);
// Lower threshold prevents lockup in the event of DNS issues.
$fp = fsockopen($parts['host'],
$errno, $errstr, 2);
$out = "POST ".$parts['path']." HTTP/1.1\r\n";
$out.= "Host: ".$parts['host']."\r\n";
$out.= "Content-Type: application/x-www-form-urlencoded\r\n";
$out.= "Content-Length: ".strlen($post_string)."\r\n";
$out.= "Connection: Close\r\n\r\n";
if (isset($post_string)) $out.= $post_string;
fwrite($fp, $out);
public static function ez_count($stat_name, $count)
self::do_async_post_request("", array('email' => self::ez_key(), 'stat' => $stat_name, 'count' => $count));
public static function ez_value($stat_name, $value)
self::do_async_post_request("", array('email' => self::ez_key(), 'stat' => $stat_name, 'value' => $value));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.