您好,欢迎访问一九零五行业门户网

分享Laravel是怎么操作宝塔面板API

本文由laravel教程栏目给大家介绍laravel+宝塔面板的相关知识,主要给大家分享laravel是怎么操作宝塔面板api,下面就带大家一起来看看,希望对需要的朋友有所帮助!
laravel 操作宝塔面板 api
不一样的地方根据自身业务修改!!!
其他的接口请查看官方文档:https://www.bt.cn/api-doc.pdf。
代码如下:
<?phpnamespace app\http\controllers\custom;use app\http\controllers\controller;use illuminate\support\facades\http;/** * 除了 addsite getssl getfilebody 外 其他都有返回 "msg" * 返回状态 "status" => true/false "msg" => "申请成功!" * 官方api文档 https://www.bt.cn/api-doc.pdf */class btpanel extends controller{ /** * 发送请求 * @param string $path /data?action=getdata&table=sites 请求路径 * @param array $query 请求参数 */ private function sendrequest(string $path, array $query) { // 宝塔面板秘钥 $secretkey = config('custom.bt.key'); // 宝塔面板地址 http://xxx.xxx.xxx:2222 填写至端口即可 $panelpath = config('custom.bt.panel_path'); $time = time(); $response = http::withoptions(['verify' => false]) ->retry(2, 5000) // !!!这里时间不适用于 getapplycert 接口 ->attach('cookie', $secretkey, 'bt.cookie') // 随便传东西就行 ->post($panelpath . $path, array_merge([ 'request_token' => md5($time . '' . md5($secretkey)), 'request_time' => $time ], $query)) ->json(); return $response ?: false; } /** * 查询网站 * @param string|null $search 需要搜索的关键词 * @return array|false */ public function sitesearch(string $search = null) { $search = $search ?: config('custom.bt.domain'); $response = $this->sendrequest('/data?action=getdata&table=sites', [ 'limit' => 5, 'search' => $search ]); // 获取失败 if (!isset($response['data'])) return false; // 不允许出现相似的网站名 if (count($response['data']) != 1) return false; $site = $response['data'][0]; return [ 'id' => $site['id'], 'name' => $site['name'], 'path' => $site['path'], 'ps' => $site['ps'], 'php' => str_replace('.', '', $site['php_version']) ]; } /** * 创建网站 * !!!ps: 使用api创建网站时 最好 不要创建相似网站名的网站 不然查询时有些麻烦 * @param string $domain 网站域名 * @param [type] json webname 网站域名 * @param [type] string path 网站路径 /www/wwwroot/www.baidu.com * @param [type] integer type_id 网站分类id * @param [type] string type 网站类型 php/java * @param [type] string version php版本 73/74 * @param [type] string port 网站端口 * @param [type] string ps 网站备注 * @param [type] bool ftp 是否创建ftp * @param [type] string ftp_username ftp用户名 // ftp为true必传 * @param [type] string ftp_password ftp密码 // ftp为true必传 * @param [type] bool sql 是否创建数据库 * @param [type] string codeing 数据库编码类型 utf8|utf8mb4|gbk|big5 // sql为true必传 * @param [type] string datauser 数据库账号 // sql为true必传 * @param [type] string datapassword 数据库密码 // sql为true必传 * @return false|int */ public function addsite(string $domain) { $data = [ 'webname' => json_encode([ 'domain' => $domain, 'domainlist' => [], 'count' => 0 ]), 'path' => config('custom.bt.site_path'), 'type_id' => '0', 'type' => 'php', 'version' => '74', 'port' => '80', 'ps' => $domain, 'ftp' => 'false', 'sql' => 'false' ]; $response = $this->sendrequest('/site?action=addsite', $data); return (isset($response['sitestatus']) && $response['sitestatus'] === true) ? (int)$response['siteid'] : false; } /** * 删除网站 * @param string $sitename 网站名称 一般是网站域名 * @return bool */ public function deletesite(string $sitename): bool { $site = $this->sitesearch($sitename); $response = $this->sendrequest('/site?action=deletesite', [ 'id' => $site['id'], 'webname' => $site['name'] ]); return isset($response['status']) && $response['status'] === true; } /** * 开启网站 * @param string $sitename 网站名称 一般是网站域名 * @return bool */ public function sitestart(string $sitename): bool { $site = $this->sitesearch($sitename); $response = $this->sendrequest('/site?action=sitestart', [ 'id' => $site['id'], 'name' => $site['name'] ]); return isset($response['status']) && $response['status'] === true; } /** * 关闭网站 * @param string $sitename 网站名称 一般是网站域名 * @return bool */ public function sitestop(string $sitename): bool { $site = $this->sitesearch($sitename); $response = $this->sendrequest('/site?action=sitestop', [ 'id' => $site['id'], 'name' => $site['name'] ]); return isset($response['status']) && $response['status'] === true; } /** * 为网站绑定域名 * @param string $sitename 网站名称 一般是网站域名 * @param string $domain 需要绑定的域名 * @return bool */ public function adddomain(string $sitename, string $domain) { $site = $this->sitesearch($sitename); $response = $this->sendrequest('/site?action=adddomain', [ 'id' => $site['id'], 'webname' => $site['name'], 'domain' => $domain ]); // 绑定成功 status === true // 绑定失败 和 指定域名已绑定过 都返回 status === false // 不好区分 失败 还是 域名已绑定 return isset($response['status']); } /** * 删除网站绑定的域名 * @param string $sitename 网站名称 一般是网站域名 * @param string $domain 需要删除的域名 * @return bool */ public function deldomain(string $sitename, string $domain) { $site = $this->sitesearch($sitename); $response = $this->sendrequest('/site?action=deldomain', [ 'id' => $site['id'], 'webname' => $site['name'], 'port' => '80', 'domain' => $domain ]); return isset($response['status']) && $response['status'] === true; } /** * 网站设置ssl证书 * @param string $domain 站点域名 * @param string $key * @param string $csr * @return bool */ public function setssl(string $domain, string $key, string $csr): bool { $data = [ 'type' => 1, 'sitename' => $domain, 'key' => '', 'csr' => '' ]; $response = $this->sendrequest('/site?action=setssl', $data); return isset($response['status']) && $response['status'] === true; } /** * 获取ssl状态及证书详情 * @param string $domain 站点域名 * @return string|false 成功则返回证书到期时间 */ public function getssl(string $domain) { $data = [ 'sitename' => $domain ]; $response = $this->sendrequest('/site?action=getssl', $data); return (isset($response['status']) && $response['status'] === true && $response['cert_data']) ? $response['cert_data']['notafter'] : false; } /** * 设置网站运行目录 * @param int $siteid 站点域名 * @param string $runpath 运行目录路径 * @return bool */ public function setsiterunpath(int $siteid, string $runpath = '/public'): bool { $data = [ 'id' => $siteid, 'runpath' => $runpath ]; $response = $this->sendrequest('/site?action=setsiterunpath', $data); return isset($response['status']) && $response['status'] === true; } /** * 获取网站预置伪静态规则内容(文件内容) * @param string $domain 网站域名 * @param [type] $type 0->获取内置伪静态规则 /www/server/panel/rewrite/nginx/xxxxx.conf;1->获取当前站点伪静态规则 /www/server/panel/vhost/rewrite/www.baidu.com.conf * @return string|false 成功则返回伪静态规则内容 */ public function getfilebody(string $domain) { $data = [ 'path' => "/www/server/panel/vhost/rewrite/$domain.conf" ]; $response = $this->sendrequest('/files?action=getfilebody', $data); return (isset($response['status']) && $response['status'] === true) ? $response['data'] : false; } /** * 保存网站伪静态规则内容(保存文件内容) * 0->系统默认路径;1->自定义全路径 * @param string $domain * @param string|null $htaccess * @return bool */ public function savefilebody(string $domain, string $htaccess = null): bool { $htaccess = $htaccess ?: config('custom.bt.htaccess'); $data = [ 'path' => "/www/server/panel/vhost/rewrite/$domain.conf", // 伪静态文件路径 'data' => $htaccess, // 伪静态规则内容 ==> 字符串 'encoding' => 'utf-8' ]; $response = $this->sendrequest('/files?action=savefilebody', $data); return isset($response['status']) && $response['status'] === true; } /** * 网站申请并设置ssl证书 * !!!ps:当前请求比较耗时间 20s-60s不等 最好单独使用 * @param int $id 站点id * @param string $domain 需要申请的域名 * @return bool|integer */ public function getapplycert(int $id, string $domain) { $data = [ "domains" => json_encode([$domain]), "auth_type" => "http", "auto_wildcard" => 0, "auth_to" => $id, "id" => $id, "sitename" => $domain ]; $response = $this->sendrequest('/acme?action=apply_cert_api', $data);// $response = [// 'cert' => '',// 'root' => '',// 'private_key' => '',// 'cert_timeout' => 1679184499,// 'status' => true// ]; if (isset($response['status']) && $response['status'] === true) { storage::put("ssl/$domain.txt", json_encode($response)); $res = $this->setssl($domain, $response['private_key'], $response['cert'] . $response['root']); return $res ? $response['cert_timeout'] : false; } return false; }}
推荐学习:《laravel视频教程》《宝塔使用教程》
以上就是分享laravel是怎么操作宝塔面板api的详细内容。
其它类似信息

推荐信息