-
Notifications
You must be signed in to change notification settings - Fork 0
/
stathat.php
88 lines (74 loc) · 2.9 KB
/
stathat.php
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
<?php
function do_post_request($url, $data, $optional_headers = null)
{
$params = array('http' => array(
'method' => 'POST',
'content' => $data
));
if ($optional_headers !== null) {
$params['http']['header'] = $optional_headers;
}
$ctx = stream_context_create($params);
$fp = @fopen($url, 'rb', false, $ctx);
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;
}
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);
$parts=parse_url($url);
$fp = fsockopen($parts['host'],
isset($parts['port'])?$parts['port']:80,
$errno, $errstr, 30);
$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);
fclose($fp);
}
function stathat_count($stat_key, $user_key, $count)
{
return do_async_post_request("http://api.stathat.com/c", array('key' => $stat_key, 'ukey' => $user_key, 'count' => $count));
}
function stathat_value($stat_key, $user_key, $value)
{
do_async_post_request("http://api.stathat.com/v", array('key' => $stat_key, 'ukey' => $user_key, 'value' => $value));
}
function stathat_ez_count($email, $stat_name, $count)
{
do_async_post_request("http://api.stathat.com/ez", array('email' => $email, 'stat' => $stat_name, 'count' => $count));
}
function stathat_ez_value($email, $stat_name, $value)
{
do_async_post_request("http://api.stathat.com/ez", array('email' => $email, 'stat' => $stat_name, 'value' => $value));
}
function stathat_count_sync($stat_key, $user_key, $count)
{
return do_post_request("http://api.stathat.com/c", "key=$stat_key&ukey=$user_key&count=$count");
}
function stathat_value_sync($stat_key, $user_key, $value)
{
return do_post_request("http://api.stathat.com/v", "key=$stat_key&ukey=$user_key&value=$value");
}
function stathat_ez_count_sync($email, $stat_name, $count)
{
return do_post_request("http://api.stathat.com/ez", "email=$email&stat=$stat_name&count=$count");
}
function stathat_ez_value_sync($email, $stat_name, $value)
{
return do_post_request("http://api.stathat.com/ez", "email=$email&stat=$stat_name&value=$value");
}
?>