|
|
[ol]'/user', 'server'=>'/server', ]; public $key; public function __construct($key) { $this->key=$key; } function curl($url, $type = 'get', $data = []) { $url = $this->getApiUrl($url); $header = [ "Authorization:Bearer {$this->key}" ]; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); if ($type == 'post') { curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); } $output = curl_exec($ch); curl_close($ch); $output= json_decode($output); if($output!=null) return $output; else return false; } function getApiUrl($key) { return online::$host.$key; } /** * 获取个人信息 * @return bool|mixed */ function getUser() { $result = $this->curl(online::$api['user']); if(is_object($result) && !isset($result->error)) return $result; else return false; } /** * 获取服务器列表,每台服务器都有详细信息 * @return array|bool */ function getServerList() { $result = $this->curl(online::$api['server']); $server_list = $result; if(empty($server_list)) { return false; } $data=[]; foreach($server_list as $server) { $arr = explode('/',$server); $server_id = $arr[4]; $data[]=$this->getServerInfo($server_id); } return $data; } /** * 获取单个服务器的信息 * @param $id * * @return bool|mixed */ function getServerInfo($id) { $result = $this->curl('/server/'.$id); if(is_object($result) && !isset($result->error)) return $result; else return false; } /** * 正常启动/救护启动 * @param $id * * @return bool|mixed */ function setBoot($id,$mode='normal') { $result = $this->curl('/server/boot/'.$mode.'/'.$id); } /** 重启 * @param $id * * @return bool|mixed */ function setReBoot($id) { $result = $this->curl('/server/reboot/'.$id); } /** * 关机 * @param $id */ function shutdown($id) { $result = $this->curl('/server/shutdown/'.$id); } } #业务流程开始 auth(); $obj = new online(API_CODE); if(isset($_GET) && !empty($_GET)) { if($_GET['op']=='normal') { $id=intval($_GET['id']); $obj->setBoot($id,'normal'); echo "
操作已执行,请耐心等待
"; } elseif($_GET['op']=='rescue') { $id=intval($_GET['id']); $obj->setBoot($id,'rescue'); echo "
操作已执行,请耐心等待
"; } elseif($_GET['op']=='reboot') { $id=intval($_GET['id']); $obj->setReboot($id); echo "
已发重启命令
"; } elseif($_GET['op']=='shutdown') { $id=intval($_GET['id']); $obj->shutdown($id); echo "
已发关机命令
"; } //@todo:这里加各项操作,重装啥的,自己补充。。 } $server_list = $obj->getServerList(); $url = 'http://'.$_SERVER["SERVER_NAME"].'/'.$_SERVER["REQUEST_URI"]; foreach($server_list as $server) { $link =[ '重启'=>$url.'?op=reboot&id='.$server->id, '关机'=>$url.'?op=shutdown&id='.$server->id, '正常模式'=>$url.'?op=normal&id='.$server->id, '救援模式'=>$url.'?op=rescue&id='.$server->id, ]; echo ''; echo '
机器ID:'.$server->id; echo '
产品规格:'.$server->offer; echo '
主机名:'.$server->hostname; echo '
操作系统:'.$server->os->name.' '.$server->os->version; echo '
电源状态:'.$server->power; echo '
当前模式:'.$server->boot_mode; echo '
滥用报告邮箱:'.$server->abuse; echo '
机器位置:'.$server->location->datacenter.' / '.$server->location->room.' / '.$server->location->rack.' / '.$server->location->block; echo '
网络信息:'.json_encode($server->network->ip); echo ''; foreach($link as $k=>$v) { echo "{$k} "; } } function auth() { $AUTH_USER = USERNAME; $AUTH_PASS = PASSWORD; header('Cache-Control: no-cache, must-revalidate, max-age=0'); $has_supplied_credentials = !(empty($_SERVER['PHP_AUTH_USER']) && empty($_SERVER['PHP_AUTH_PW'])); $is_not_authenticated = ( !$has_supplied_credentials || $_SERVER['PHP_AUTH_USER'] != $AUTH_USER || $_SERVER['PHP_AUTH_PW'] != $AUTH_PASS ); if ($is_not_authenticated) { header('HTTP/1.1 401 Authorization Required'); header('WWW-Authenticate: Basic realm="Access denied"'); exit; } }[/ol]复制代码 |
|