- <?php
- /**
- * 宝塔API接口示例Demo
- * 仅供参考,请根据实际项目需求开发,并做好安全处理
- * date 2018/12/12
- * author 阿良
- */
- class bt_api {
- private $BT_KEY = "T******************************h"; //接口密钥
- private $BT_PANEL = "http://u.************.net:8888"; //面板地址
-
- //如果希望多台面板,可以在实例化对象时,将面板地址与密钥传入
- public function __construct($bt_panel = null,$bt_key = null){
- if($bt_panel) $this->BT_PANEL = $bt_panel;
- if($bt_key) $this->BT_KEY = $bt_key;
- }
-
- //示例取面板日志
- public function GetLogs(){
- //拼接URL地址
- $url = $this->BT_PANEL.'/data?action=getData';
-
- //准备POST数据
- $p_data = $this->GetKeyData(); //取签名
- $p_data['table'] = 'logs';
- $p_data['limit'] = 10;
- $p_data['tojs'] = 'test';
-
- //请求面板接口
- $result = $this->HttpPostCookie($url,$p_data);
-
- //解析JSON数据
- $data = json_decode($result,true);
- return $data;
- }
-
-
- /**
- * 构造带有签名的关联数组
- */
- private function GetKeyData(){
- $now_time = time();
- $p_data = array(
- 'request_token' => md5($now_time.''.md5($this->BT_KEY)),
- 'request_time' => $now_time,
- //'panel_address'=>"lwsd.why.showcai.net",
- //'panel_port'=>8888,
- );
- return $p_data;
- }
-
-
- /**
- * 发起POST请求
- * @param String $url 目标网填,带http://
- * @param Array|String $data 欲提交的数据
- * @return string
- */
- private function HttpPostCookie($url, $data,$timeout = 60)
- {
- //定义cookie保存位置
- $cookie_file='./'.md5($this->BT_PANEL).'.cookie';
- if(!file_exists($cookie_file)){
- $fp = fopen($cookie_file,'w+');
- fclose($fp);
- }
-
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
- curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
- curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- $output = curl_exec($ch);
- curl_close($ch);
- return $output;
- }
-
-
-
-
- // 安全 - 系统防火墙 - 端口转发
- public function forward($ip="",$type=0,$id=1){
- //拼接URL地址
- $url= "";
- $p_data = $this->GetKeyData(); //取签名
- if($ip==""){//没有ip便取列表
- $url= $this->BT_PANEL."/safe/firewall/get_forward_list";
- $p_data['p'] = '1';
- }else{//有ip便添加或修改
- $url = $this->BT_PANEL.'/safe/firewall/'.( $type? "create_forward":"modify_forward" );
- if($type!=1) $p_data['id'] = $id;
- $p_data['protocol'] = "tcp/udp";
- $p_data['s_ports'] = '4431';
- $p_data['d_address'] = $ip;
- $p_data['d_ports'] = '80';
- }
-
-
- //请求面板接口
- $result = $this->HttpPostCookie($url,$p_data);
- echo "\r\n";
- var_dump($url);
- echo "\r\n";
- var_dump($p_data);
- echo "\r\n";
- var_dump($result);
- echo "\r\n";
- //解析JSON数据
- $data = json_decode($result,true);
- return $data;
- }
-
- }
- //实例化对象
- $api = new bt_api();
- echo "<pre>\r\n";
- //获取面板日志
- //$r_data = $api->GetLogs();
- //输出JSON数据到浏览器
- //var_dump ($r_data);
- $r_data = $api->forward();//有返回结果,说明接口白名单及其他参数没问题。
- var_dump ($r_data) ;
- echo "\r\n\r\n";
- $r_data = $api->forward("127.0.0.2",0,4);//无返回结果且无更改,说明该接口有问题。安全 - 系统防火墙 - 端口转发 【ip,增/改,要改的序号】
- var_dump($r_data);
- echo "\r\n\r\n</pre>";
- ?>
复制代码
https://www.bt.cn/bbs/forum.php?mod=attachment&aid=MTI2MjQzfDY1MTYzOGFkZjk0MDY0MWQwNTJhZjg3MDU5ZTc1ODE2fDE3Mzc2NDI2MjM%3D&request=yes&_f=.zip
|