-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurl.php
152 lines (130 loc) · 4.73 KB
/
curl.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
header('Content-type: application/json; charset=UTF-8');
//登陆并获取cookie
function login($email,$password){
$login_url='https://app.arukas.io/api/login';
$postdata = array('email' =>$email ,'password'=>$password);
$cookie_file = dirname(__FILE__).'/cookie.txt';
$login=curl_init($login_url);
curl_setopt($login,CURLOPT_HEADER,0);
curl_setopt($login,CURLOPT_RETURNTRANSFER,true);
curl_setopt($login,CURLOPT_POST,1);
curl_setopt($login,CURLOPT_POSTFIELDS,$postdata);
//curl_setopt($login,CURLOPT_SSL_VERIFYPEER,false); //忽略SSL证书
curl_setopt($login,CURLOPT_COOKIEJAR,$cookie_file);
$api = curl_exec($login);
curl_close($login);
return $api;
}
//利用cookie获取信息
function getMyinfo(){
$url='https://app.arukas.io/api/me';
$cookie_file = dirname(__FILE__).'/cookie.txt';
$curl=curl_init($url);
curl_setopt($curl,CURLOPT_HEADER, 0);
curl_setopt($curl,CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl,CURLOPT_COOKIEFILE, $cookie_file);
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER, false);
$api=curl_exec($curl);
curl_close($curl);
return $api;
}
function getApps(){
$url='https://app.arukas.io/api/apps';
$cookie_file = dirname(__FILE__).'/cookie.txt';
$curl=curl_init($url);
curl_setopt($curl,CURLOPT_HEADER, 0);
curl_setopt($curl,CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl,CURLOPT_COOKIEFILE, $cookie_file);
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER, false);
$api=curl_exec($curl);
curl_close($curl);
return $api;
}
function getContainer(){
$url='https://app.arukas.io/api/containers';
$cookie_file = dirname(__FILE__).'/cookie.txt';
$curl=curl_init($url);
curl_setopt($curl,CURLOPT_HEADER, 0);
curl_setopt($curl,CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl,CURLOPT_COOKIEFILE, $cookie_file);
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER, false);
$api=curl_exec($curl);
curl_close($curl);
return $api;
}
function setApp($name){
$postData = ["data"=>[
0=>[
"type"=>"containers",
"attributes"=>[
"image_name"=>"mo2017/shadowsocks",
"instances"=> 1,
"mem"=> 256,
"cmd"=> "",
"envs"=>[
0=>[
"key"=>"PASS",
"value"=> "601qaz106"
]
],
"ports"=>[
0=>[
"number"=> 8388,
"protocol"=> "tcp"
]
],
"arukas_domain"=> ""
]
],
1=>[
"type"=>"apps",
"attributes"=>[
"name"=> $name
]
]
],
];
$postData = json_encode($postData);
//php内置curl方法传参后,服务器应答为 500 服务器内部错误,故改用shell命令直接调用linux curl
// $url='https://app.arukas.io/api/app-sets';
// printf($postData);
// $curl=curl_init($url);
// curl_setopt($curl,CURLOPT_NETRC,1);
// // curl_setopt(CURL *handle, CURLOPT_NETRC_FILE, char *file);
// curl_setopt($curl,CURLOPT_HEADER,0);
// curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
// curl_setopt($curl,CURLOPT_POST,1);
// curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
// curl_setopt($curl,CURLOPT_POSTFIELDS,$postData);
// curl_setopt($curl, CURLOPT_HTTPHEADER, ["Content-Type: application/vnd.api+json","Accept: application/vnd.api+json"]);
// $api = curl_exec($curl);
// curl_close($curl);
//$shell 中不能有 \\ 回车 等符号 否则无法正常输出命令!!!
$shell = "curl -n -X POST https://app.arukas.io/api/app-sets -d '$postData' -H \"Content-Type: application/vnd.api+json\" -H \"Accept: application/vnd.api+json\" ";
//printf($shell);
$api = shell_exec($shell);
return $api;
}
function startApp($id){
$url='https://app.arukas.io/api/containers/'.$id.'/power';
$curl=curl_init($url);
curl_setopt($curl,CURLOPT_NETRC,1);
curl_setopt($curl,CURLOPT_HEADER,0);
curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl,CURLOPT_POST,1);
curl_setopt($curl, CURLOPT_HTTPHEADER, ["Content-Type: application/vnd.api+json","Accept: application/vnd.api+json"]);
$api = curl_exec($curl);
curl_close($curl);
return $api;
}
function getPort(){
$container = json_decode(getContainer(),true);
//var_dump($container);
foreach ($container["data"] as $item => $value){
//var_dump($value);
$service_port = $value["attributes"]["port_mappings"][0][0]["service_port"];
$host = $value["attributes"]["port_mappings"][0][0]["host"];
print($host."\n".$service_port."\n");
}
}