-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKoronaApi.class.php
92 lines (70 loc) · 2.1 KB
/
KoronaApi.class.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
89
90
91
92
<?php
class KoronaApi
{
const URL = "https://www.koronacloud.com/api";
/**
* get the token
* Please be aware that this mathod can only be called once per api key. You need to store the returned token in a secured place.
* @return (string) token
*/
public static function auth($appId, $secret, $apiKey)
{
$token = file_get_contents(self::URL."/v1/auth/$appId/$secret/$apiKey");
return $token;
}
private $token;
public function __construct($token)
{
if (!isset($token) || empty($token))
throw new Exception("Missing token");
if (strlen($token) < 10)
throw new Exception("Invalid token");
$this->token = $token;
}
public function getObjByNumber($type, $number)
{
$content = file_get_contents(self::URL."/v1/".$this->token."/".$type."/number/$number");
$result = json_decode($content);
if (isset($result->error))
throw new Exception($result->error);
return $result->result;
}
public function getObjById($type, $uuid)
{
$content = file_get_contents(self::URL."/v1/".$this->token."/".$type."/id/$uuid");
$result = json_decode($content);
if (isset($result->error))
throw new Exception($result->error);
return $result->result;
}
public function getUpdates($type, $revision, $limit, $offset)
{
$content = file_get_contents(self::URL."/v1/".$this->token."/".$type."/updates/$revision/$limit/$offset");
$result = json_decode($content);
if (isset($result->error))
throw new Exception($result->error);
return $result->resultList;
}
public function save($type, $obj)
{
return $this->post($type."/save/", $obj);
}
public function post($path, $obj)
{
$content = json_encode($obj);
$options = array(
'http' => array(
'header' => "Content-type: application/json\r\n",
'method' => 'POST',
'content' => $content,
),
);
$context = stream_context_create($options);
$content = file_get_contents(self::URL."/v1/".$this->token."/".$path, false, $context);
$result = json_decode($content);
if (isset($result->error))
throw new Exception($result->error);
return $result->result;
}
}
?>