-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.php
79 lines (70 loc) · 1.9 KB
/
api.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
<?
class api
{
private static $base_url = NULL;
private static $key = NULL;
private static $secret = NULL;
public static $error = NULL;
public static $response = NULL;
public static function set_base_url($url)
{
self::$base_url = $url;
}
public static function set_api_key($key, $secret)
{
self::$key = $key;
self::$secret = $secret;
}
public static function get($controller, $action, $params = array(), $print_url = FALSE)
{
$params['_key'] = self::$key;
$url = self::$base_url . "/{$controller}/{$action}?" . http_build_query($params);
if($print_url === TRUE)
{
print $url;
}
return self::process_response(curl::get($url));
}
public static function post($controller, $action, $params = array())
{
$params['_key'] = self::$key;
$url = self::$base_url . "/{$controller}/{$action}";
return self::process_response(curl::post($url, $params));
}
public static function process_response($response)
{
$response = json_decode($response, TRUE);
self::$response = $response;
if($response == NULL)
{
self::$error = "Invalid JSON returned";
return FALSE;
}
elseif($response['success'] == 0)
{
self::$error = $response['error'];
return FALSE;
}
else
{
return ((isset($response['data'])) ? $response['data'] : TRUE);
}
}
public static function get_param_errors()
{
if(isset(self::$response['param_errors']))
{
return self::$response['param_errors'];
}
return array();
}
public static function get_error()
{
if(isset(self::$response['error']))
{
return self::$response['error'];
}
return NULL;
}
}
?>