<?php @header("content-type:text/html;charset=UTF-8"); if(isset($_REQUEST['p'])){ $p = $_REQUEST ['p']; }else{ $p = 's'; } if(isset($_REQUEST['d'])){ $d = $_REQUEST ['d']; }else{ $d = 't'; } //登陆函数 $urlQian = 'http://XXXXXXXXXXXXX:8081'; //账户信息地址 $user = 'XXXXXXXX'; //账户信息 $password = 'XXXXXX'; //账户信息 $postUrl = $urlQian.'/login.cgi'; function json_to_array($data){ $data = json_decode($data,JSON_UNESCAPED_UNICODE); return $data; } function array_to_json($data){ $data = json_encode($data,JSON_UNESCAPED_UNICODE); return $data; } function Sec2Time($allSec){ $remainSec = (int)$allSec; $humanTime = ''; if($remainSec > 86400) { $days = (int)($remainSec / 86400); $remainSec = $remainSec % 86400; $humanTime .= $days.'天'; } if($remainSec > 3600) { $hours = (int)($remainSec / 3600); $remainSec = $remainSec % 3600; $humanTime .= $hours.'时'; } if($remainSec > 60) { $minutes = (int)($remainSec / 60); $remainSec = $remainSec % 60; $humanTime .= $minutes.'分'; } if($remainSec > 0) { $seconds = $remainSec; $humanTime .= $seconds.'秒'; } return $humanTime; } function http_json_data($postUrl, $curlPost, $cookiesState) { if (empty($postUrl) || empty($curlPost)) { return false; } $cookie_file = dirname(__FILE__).'/cookie.txt'; $ch = curl_init(); $header = array(); $header[] = 'content-type: application/x-www-form-urlencoded'; $header[] = 'charset=UTF-8'; curl_setopt($ch, CURLOPT_URL,$postUrl); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost); if ($cookiesState==1){ curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file); }else{ curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); } curl_setopt($ch, CURLOPT_HTTPHEADER, $header); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); $data = curl_exec($ch); $data = iconv('gbk','utf-8', $data); curl_close($ch); return $data; } function lidData($urlQian,$cookiesState=0) { $postUrl=$urlQian.'/dhcp_list.data'; $curlPost = '_=1668761642829'; $lidData = http_json_data($postUrl, $curlPost , $cookiesState=0); $lidData= json_to_array($lidData); return $lidData; } $curlPost='user='.$user.'&password='.$password.'&checkCode=&key=&Submit=%E7%99%BB++%E5%BD%95'; $login = http_json_data($postUrl, $curlPost,$cookiesState=1); //模拟登陆 //var_dump($login); if(strpos($login,'login.html?flag=0') !== false){ @header("content-type:application/json;charset=UTF-8"); echo '{"state":"0","key":"账户密码错误"}'; }elseif(strpos($login,'login.html?flag=1') !== false){ @header("content-type:application/json;charset=UTF-8"); echo '{"state":"0","key":"验证码错误"}'; }else{ $data = lidData($urlQian,$cookiesState=0); //print_r($data); if($p == 's'){ $n_time = array_column($data,$d); array_multisort($n_time,SORT_ASC,$data); $p='j'; //echo $p; }else{ $n_time = array_column($data,$d); array_multisort($n_time,SORT_DESC,$data); $p='s'; //echo $p; } echo '<p align="center" style="font-size: 22px;">合计有'.sizeof($data).'台设备在线</p>'; echo '<table border="1" style="margin:auto;table-layout:fixed;font-size:12px;height: 300px;" cellspacing="0"><tr><th>序号</th><th><a href="?d=n&p='.$p.'">名称</a></th><th><a href="?d=m&p='.$p.'">MAC地址</a></th><th><a href="?d=i&p='.$p.'">IP地址</a></th><th><a href="?d=t&p='.$p.'">在线时间</a></th></tr><tr>'; foreach($data as $key => $val ){ $id_num = $key +1; $id_num = ($id_num>9)?$id_num:'0'.$id_num; echo '<tr style="text-align:center;height: 30px;"><td>'.$id_num.'</td><td>'.$val['n'].'</td><td>'.$val['m'].'</td><td>'.long2ip($val['i']).'</td><td>'.Sec2Time($val['t']).'</td></tr>'; } echo '</tr></table>'; } ?>
标签:ch,setopt,WAYOS,remainSec,JSON,curl,PHP,data,CURLOPT From: https://www.cnblogs.com/zhouein/p/16908439.html