站長資訊網
        最全最豐富的資訊網站

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

        本文由Laravel教程欄目給大家介紹laravel+寶塔面板的相關知識,主要給大家分享Laravel是怎么操作寶塔面板API,下面就帶大家一起來看看,希望對需要的朋友有所幫助!

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

        Laravel 操作寶塔面板 API

        不一樣的地方根據自身業務修改!!!

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

        代碼如下:

        <?php namespace AppHttpControllersCustom; use AppHttpControllersController; use IlluminateSupportFacadesHttp; /**  * 除了 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視頻教程》《寶塔使用教程》

        贊(0)
        分享到: 更多 (0)
        網站地圖   滬ICP備18035694號-2    滬公網安備31011702889846號
        主站蜘蛛池模板: 尤物国精品午夜福利视频| 久久伊人精品青青草原日本 | 国产精品久久免费| 国产精品莉莉欧美自在线线| 亚洲精品国产日韩无码AV永久免费网| 宅男在线国产精品无码| 亚洲国产精品一区| 久久这里只有精品视频99| 亚洲欧洲国产日韩精品| 国产精品免费高清在线观看| 国产国拍亚洲精品福利| 四库影院永久四虎精品国产 | 亚洲精品无码日韩国产不卡?V | 亚洲第一区精品日韩在线播放| segui久久国产精品| 亚洲国产精品嫩草影院久久| 国产在线精品福利大全| 少妇精品久久久一区二区三区| 国产精品视频一区二区三区| 人人妻人人澡人人爽人人精品电影| 久久精品九九亚洲精品天堂| 日韩一区二区精品观看| 国产三级国产精品国产普男人| 少妇人妻精品一区二区三区| 亚洲精品国产精品国自产观看| 久久99精品久久久久久不卡| 97久久超碰国产精品旧版| 国内精品国产成人国产三级| 五月花精品视频在线观看| 国产伦精品一区二区三区女| 99热亚洲色精品国产88| 国产精品vⅰdeoxxxx国产| 精品福利视频一区二区三区| 国产精品亚洲产品一区二区三区| 91精品国产麻豆国产自产在线| 亚洲国产精品无码久久久秋霞2| 欧美日韩在线亚洲国产精品| 国产精品亚洲欧美一区麻豆| 99精品国产一区二区三区| 亚洲精品亚洲人成在线观看下载| 亚洲AV永久无码精品一区二区|