-
Notifications
You must be signed in to change notification settings - Fork 1
/
cloudAPI.php
125 lines (107 loc) · 2.82 KB
/
cloudAPI.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
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
<?php
require 'vendor/autoload.php';
/**
* Class Chunxi
*/
class Chunxi
{
const API_URL = "https://api.onetap.com/cloud/";
//Please go to the Personal Center to obtain this information before using it.
const API_ID = "";
const API_SECRET = "";
const API_KEY = "";
/**
* @var \GuzzleHttp\Client
*/
protected \GuzzleHttp\Client $client;
/**
* Chunxi constructor.
*/
public function __construct()
{
$this->client = new \GuzzleHttp\Client([
"base_uri" => self::API_URL,
"timeout" => 5.0,
"headers" => [
"X-Api-Id" => self::API_ID,
"X-Api-Secret" => self::API_SECRET,
"X-Api-Key" => self::API_KEY
]
]);
}
/**
* @param string $uri
* @return string
*/
protected function getUri(string $uri): string
{
if (substr($uri, 0, strlen($uri) - 1) != "/") {
$uri .= "/";
}
return $uri;
}
/**
* @param string $uri
* @return array
*/
public function get(string $uri): ?array
{
try {
$response = $this->client->get($this->getUri($uri));
return json_decode($response->getBody()->getContents(), true);
} catch (Exception $e) {
return null;
}
}
/**
* @param string $uri
* @param array $data
* @return array|mixed
*/
public function post(string $uri, array $data): ?array
{
try {
$response = $this->client->post($this->getUri($uri), [
"form_params" => $data,
"headers" => [
"Content-Type" => "application/x-www-form-urlencoded"
]
]);
return json_decode($response->getBody()->getContents(), true);
} catch (Exception $e) {
return null;
}
}
/**
* @param string $uri
* @param array $data
* @return array|mixed
*/
public function delete(string $uri, array $data): ?array
{
try {
$response = $this->client->delete($this->getUri($uri), [
"form_params" => $data,
"headers" => [
"Content-Type" => "application/x-www-form-urlencoded"
]
]);
return json_decode($response->getBody()->getContents(), true);
} catch (Exception $e) {
return null;
}
}
}
$chunxi = new Chunxi();
//Gets all configs.
$configs = $chunxi->get("configs");
var_dump($configs);
//Creates a new script invite.
$invites = $chunxi->post("scripts/41458/invites", [
"max_age" => 1,
"max_uses" => 1
]);
var_dump($invites);
//Deletes a script invite.
$delete = $chunxi->delete("scripts/41458/invites", ["invite_id" => "12269"]);
var_dump($delete);