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

 3222

本文由Laravel教程栏目给大家介绍laravel+宝塔面板的相关知识,主要给大家分享Laravel是怎么操作宝塔面板API,下面就带大家一起来看看,希望对需要的朋友有所帮助!


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


Laravel 操作宝塔面板 API

不一样的地方根据自身业务修改!!!

其他的接口请查看官方文档:https://www.bt.cn/api-doc.pdf

代码如下:

  1. <?php
  2. namespace App\Http\Controllers\Custom;
  3. use App\Http\Controllers\Controller;
  4. use Illuminate\Support\Facades\Http;
  5. /**
  6.  * 除了 AddSite GetSSL GetFileBody 外  其他都有返回 "msg"
  7.  * 返回状态 "status" => true/false  "msg" => "申请成功!"
  8.  * 官方API文档  https://www.bt.cn/api-doc.pdf
  9.  */
  10. class BtPanel extends Controller
  11. {
  12.     /**
  13.      * 发送请求
  14.      * @param string $path /data?action=getData&table=sites 请求路径
  15.      * @param array $query 请求参数
  16.      */
  17.     private function sendRequest(string $path, array $query)
  18.     {
  19.         // 宝塔面板秘钥
  20.         $secretKey = config('custom.bt.key');
  21.         // 宝塔面板地址 http://xxx.xxx.xxx:2222 填写至端口即可
  22.         $panelPath = config('custom.bt.panel_path');
  23.         $time = time();
  24.         $response = Http::withOptions(['verify' => false])
  25.             ->retry(2, 5000) // !!!这里时间不适用于 GetApplyCert 接口
  26.             ->attach('cookie', $secretKey, 'bt.cookie') // 随便传东西就行
  27.             ->post($panelPath . $path, array_merge([
  28.                 'request_token' => md5($time . '' . md5($secretKey)),
  29.                 'request_time' => $time
  30.             ], $query))
  31.             ->json();
  32.         return $response ?: false;
  33.     }
  34.     /**
  35.      * 查询网站
  36.      * @param string|null $search 需要搜索的关键词
  37.      * @return array|false
  38.      */
  39.     public function SiteSearch(string $search = null)
  40.     {
  41.         $search = $search ?: config('custom.bt.domain');
  42.         $response = $this->sendRequest('/data?action=getData&table=sites', [
  43.             'limit' => 5,
  44.             'search' => $search
  45.         ]);
  46.         // 获取失败
  47.         if (!isset($response['data'])) return false;
  48.         // 不允许出现相似的网站名
  49.         if (count($response['data']) != 1) return false;
  50.         $site = $response['data'][0];
  51.         return [
  52.             'id' => $site['id'],
  53.             'name' => $site['name'],
  54.             'path' => $site['path'],
  55.             'ps' => $site['ps'],
  56.             'php' => str_replace('.', '', $site['php_version'])
  57.         ];
  58.     }
  59.     /**
  60.      * 创建网站
  61.      * !!!PS: 使用API创建网站时  最好 不要创建相似网站名的网站  不然查询时有些麻烦
  62.      * @param string $domain 网站域名
  63.      * @param [type] json webname        网站域名
  64.      * @param [type] string path         网站路径 /www/wwwroot/www.baidu.com
  65.      * @param [type] integer type_id     网站分类ID
  66.      * @param [type] string type         网站类型 PHP/JAVA
  67.      * @param [type] string version      PHP版本 73/74
  68.      * @param [type] string port         网站端口
  69.      * @param [type] string ps           网站备注
  70.      * @param [type] bool ftp            是否创建FTP
  71.      * @param [type] string ftp_username  FTP用户名 // ftp为true必传
  72.      * @param [type] string ftp_password  FTP密码  // ftp为true必传
  73.      * @param [type] bool sql            是否创建数据库
  74.      * @param [type] string codeing      数据库编码类型 utf8|utf8mb4|gbk|big5  // sql为true必传
  75.      * @param [type] string datauser     数据库账号 // sql为true必传
  76.      * @param [type] string datapassword 数据库密码 // sql为true必传
  77.      * @return false|int
  78.      */
  79.     public function AddSite(string $domain)
  80.     {
  81.         $data = [
  82.             'webname' => json_encode([
  83.                 'domain' => $domain,
  84.                 'domainlist' => [],
  85.                 'count' => 0
  86.             ]),
  87.             'path' => config('custom.bt.site_path'),
  88.             'type_id' => '0',
  89.             'type' => 'PHP',
  90.             'version' => '74',
  91.             'port' => '80',
  92.             'ps' => $domain,
  93.             'ftp' => 'false',
  94.             'sql' => 'false'
  95.         ];
  96.         $response = $this->sendRequest('/site?action=AddSite', $data);
  97.         return (isset($response['siteStatus']) && $response['siteStatus'] === true) ? (int)$response['siteId'] : false;
  98.     }
  99.     /**
  100.      * 删除网站
  101.      * @param string $siteName 网站名称 一般是网站域名
  102.      * @return bool
  103.      */
  104.     public function DeleteSite(string $siteName): bool
  105.     {
  106.         $site = $this->SiteSearch($siteName);
  107.         $response = $this->sendRequest('/site?action=DeleteSite', [
  108.             'id' => $site['id'],
  109.             'webname' => $site['name']
  110.         ]);
  111.         return isset($response['status']) && $response['status'] === true;
  112.     }
  113.     /**
  114.      * 开启网站
  115.      * @param string $siteName 网站名称 一般是网站域名
  116.      * @return bool
  117.      */
  118.     public function SiteStart(string $siteName): bool
  119.     {
  120.         $site = $this->SiteSearch($siteName);
  121.         $response = $this->sendRequest('/site?action=SiteStart', [
  122.             'id' => $site['id'],
  123.             'name' => $site['name']
  124.         ]);
  125.         return isset($response['status']) && $response['status'] === true;
  126.     }
  127.     /**
  128.      * 关闭网站
  129.      * @param string $siteName 网站名称 一般是网站域名
  130.      * @return bool
  131.      */
  132.     public function SiteStop(string $siteName): bool
  133.     {
  134.         $site = $this->SiteSearch($siteName);
  135.         $response = $this->sendRequest('/site?action=SiteStop', [
  136.             'id' => $site['id'],
  137.             'name' => $site['name']
  138.         ]);
  139.         return isset($response['status']) && $response['status'] === true;
  140.     }
  141.     /**
  142.      * 为网站绑定域名
  143.      * @param string $siteName 网站名称 一般是网站域名
  144.      * @param string $domain 需要绑定的域名
  145.      * @return bool
  146.      */
  147.     public function AddDomain(string $siteName, string $domain)
  148.     {
  149.         $site = $this->SiteSearch($siteName);
  150.         $response = $this->sendRequest('/site?action=AddDomain', [
  151.             'id' => $site['id'],
  152.             'webname' => $site['name'],
  153.             'domain' => $domain
  154.         ]);
  155.         // 绑定成功 status === true
  156.         // 绑定失败 和 指定域名已绑定过  都返回 status === false
  157.         // 不好区分 失败 还是 域名已绑定
  158.         return isset($response['status']);
  159.     }
  160.     /**
  161.      * 删除网站绑定的域名
  162.      * @param string $siteName 网站名称 一般是网站域名
  163.      * @param string $domain 需要删除的域名
  164.      * @return bool
  165.      */
  166.     public function DelDomain(string $siteName, string $domain)
  167.     {
  168.         $site = $this->SiteSearch($siteName);
  169.         $response = $this->sendRequest('/site?action=DelDomain', [
  170.             'id' => $site['id'],
  171.             'webname' => $site['name'],
  172.             'port' => '80',
  173.             'domain' => $domain
  174.         ]);
  175.         return isset($response['status']) && $response['status'] === true;
  176.     }
  177.     /**
  178.      * 网站设置SSL证书
  179.      * @param string $domain 站点域名
  180.      * @param string $key
  181.      * @param string $csr
  182.      * @return bool
  183.      */
  184.     public function SetSSL(string $domain, string $key, string $csr): bool
  185.     {
  186.         $data = [
  187.             'type' => 1,
  188.             'siteName' => $domain,
  189.             'key' => '',
  190.             'csr' => ''
  191.         ];
  192.         $response = $this->sendRequest('/site?action=SetSSL', $data);
  193.         return isset($response['status']) && $response['status'] === true;
  194.     }
  195.     /**
  196.      * 获取SSL状态及证书详情
  197.      * @param string $domain 站点域名
  198.      * @return string|false 成功则返回证书到期时间
  199.      */
  200.     public function GetSSL(string $domain)
  201.     {
  202.         $data = [
  203.             'siteName' => $domain
  204.         ];
  205.         $response = $this->sendRequest('/site?action=GetSSL', $data);
  206.         return (isset($response['status']) && $response['status'] === true && $response['cert_data']) ? $response['cert_data']['notAfter'] : false;
  207.     }
  208.     /**
  209.      * 设置网站运行目录
  210.      * @param int $siteId 站点域名
  211.      * @param string $runPath 运行目录路径
  212.      * @return bool
  213.      */
  214.     public function SetSiteRunPath(int $siteId, string $runPath = '/public'): bool
  215.     {
  216.         $data = [
  217.             'id' => $siteId,
  218.             'runPath' => $runPath
  219.         ];
  220.         $response = $this->sendRequest('/site?action=SetSiteRunPath', $data);
  221.         return isset($response['status']) && $response['status'] === true;
  222.     }
  223.     /**
  224.      * 获取网站预置伪静态规则内容(文件内容)
  225.      * @param string $domain 网站域名
  226.      * @param [type] $type 0->获取内置伪静态规则 /www/server/panel/rewrite/nginx/xxxxx.conf;1->获取当前站点伪静态规则 /www/server/panel/vhost/rewrite/www.baidu.com.conf
  227.      * @return string|false 成功则返回伪静态规则内容
  228.      */
  229.     public function GetFileBody(string $domain)
  230.     {
  231.         $data = [
  232.             'path' => "/www/server/panel/vhost/rewrite/$domain.conf"
  233.         ];
  234.         $response = $this->sendRequest('/files?action=GetFileBody', $data);
  235.         return (isset($response['status']) && $response['status'] === true) ? $response['data'] : false;
  236.     }
  237.     /**
  238.      * 保存网站伪静态规则内容(保存文件内容)
  239.      * 0->系统默认路径;1->自定义全路径
  240.      * @param string $domain
  241.      * @param string|null $htaccess
  242.      * @return bool
  243.      */
  244.     public function SaveFileBody(string $domain, string $htaccess = null): bool
  245.     {
  246.         $htaccess = $htaccess ?: config('custom.bt.htaccess');
  247.         $data = [
  248.             'path' => "/www/server/panel/vhost/rewrite/$domain.conf", // 伪静态文件路径
  249.             'data' => $htaccess, // 伪静态规则内容 ==> 字符串
  250.             'encoding' => 'utf-8'
  251.         ];
  252.         $response = $this->sendRequest('/files?action=SaveFileBody', $data);
  253.         return isset($response['status']) && $response['status'] === true;
  254.     }
  255.     /**
  256.      * 网站申请并设置SSL证书
  257.      * !!!PS:当前请求比较耗时间 20s-60s不等  最好单独使用
  258.      * @param int $id 站点ID
  259.      * @param string $domain 需要申请的域名
  260.      * @return bool|integer
  261.      */
  262.     public function GetApplyCert(int $id, string $domain)
  263.     {
  264.         $data = [
  265.             "domains" => json_encode([$domain]),
  266.             "auth_type" => "http",
  267.             "auto_wildcard" => 0,
  268.             "auth_to" => $id,
  269.             "id" => $id,
  270.             "siteName" => $domain
  271.         ];
  272.         $response = $this->sendRequest('/acme?action=apply_cert_api', $data);
  273. //        $response = [
  274. //            'cert' => '',
  275. //            'root' => '',
  276. //            'private_key' => '',
  277. //            'cert_timeout' => 1679184499,
  278. //            'status' => true
  279. //        ];
  280.         if (isset($response['status']) && $response['status'] === true) {
  281.             Storage::put("ssl/$domain.txt", json_encode($response));
  282.             $res = $this->SetSSL($domain, $response['private_key'], $response['cert'] . $response['root']);
  283.             return $res ? $response['cert_timeout'] : false;
  284.         }
  285.         return false;
  286.     }
  287. }


本文网址:https://www.zztuku.com/detail-13656.html
站长图库 - 分享Laravel是怎么操作宝塔面板API
申明:本文转载于《learnku》,如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐