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

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

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

Laravel 操作寶塔面板 API

不一樣的地方根據(jù)自身業(yè)務(wù)修改!!!

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

代碼如下:

<?php namespace AppHttpControllersCustom; use AppHttpControllersController; use IlluminateSupportFacadesHttp; /**  * 除了 AddSite GetSSL GetFileBody 外  其他都有返回 "msg"  * 返回狀態(tài) "status" =>?true/false??"msg"?=&gt;?"申請成功!" ?*?官方API文檔??https://www.bt.cn/api-doc.pdf ?*/ class?BtPanel?extends?Controller { ????/** ?????*?發(fā)送請求 ?????*?@param?string?$path?/data?action=getData&amp;table=sites?請求路徑 ?????*?@param?array?$query?請求參數(shù) ?????*/ ????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'?=&gt;?false]) ????????????-&gt;retry(2,?5000)?//?!!!這里時(shí)間不適用于?GetApplyCert?接口 ????????????-&gt;attach('cookie',?$secretKey,?'bt.cookie')?//?隨便傳東西就行 ????????????-&gt;post($panelPath?.?$path,?array_merge([ ????????????????'request_token'?=&gt;?md5($time?.?''?.?md5($secretKey)), ????????????????'request_time'?=&gt;?$time ????????????],?$query)) ????????????-&gt;json(); ????????return?$response??:?false; ????} ????/** ?????*?查詢網(wǎng)站 ?????*?@param?string|null?$search?需要搜索的關(guān)鍵詞 ?????*?@return?array|false ?????*/ ????public?function?SiteSearch(string?$search?=?null) ????{ ????????$search?=?$search??:?config('custom.bt.domain'); ????????$response?=?$this-&gt;sendRequest('/data?action=getData&amp;table=sites',?[ ????????????'limit'?=&gt;?5, ????????????'search'?=&gt;?$search ????????]); ????????//?獲取失敗 ????????if?(!isset($response['data']))?return?false; ????????//?不允許出現(xiàn)相似的網(wǎng)站名 ????????if?(count($response['data'])?!=?1)?return?false; ????????$site?=?$response['data'][0]; ????????return?[ ????????????'id'?=&gt;?$site['id'], ????????????'name'?=&gt;?$site['name'], ????????????'path'?=&gt;?$site['path'], ????????????'ps'?=&gt;?$site['ps'], ????????????'php'?=&gt;?str_replace('.',?'',?$site['php_version']) ????????]; ????} ????/** ?????*?創(chuàng)建網(wǎng)站 ?????*?!!!PS:?使用API創(chuàng)建網(wǎng)站時(shí)??最好?不要?jiǎng)?chuàng)建相似網(wǎng)站名的網(wǎng)站??不然查詢時(shí)有些麻煩 ?????*?@param?string?$domain?網(wǎng)站域名 ?????*?@param?[type]?json?webname????????網(wǎng)站域名 ?????*?@param?[type]?string?path?????????網(wǎng)站路徑?/www/wwwroot/www.baidu.com ?????*?@param?[type]?integer?type_id?????網(wǎng)站分類ID ?????*?@param?[type]?string?type?????????網(wǎng)站類型?PHP/JAVA ?????*?@param?[type]?string?version??????PHP版本?73/74 ?????*?@param?[type]?string?port?????????網(wǎng)站端口 ?????*?@param?[type]?string?ps???????????網(wǎng)站備注 ?????*?@param?[type]?bool?ftp????????????是否創(chuàng)建FTP ?????*?@param?[type]?string?ftp_username??FTP用戶名?//?ftp為true必傳 ?????*?@param?[type]?string?ftp_password??FTP密碼??//?ftp為true必傳 ?????*?@param?[type]?bool?sql????????????是否創(chuàng)建數(shù)據(jù)庫 ?????*?@param?[type]?string?codeing??????數(shù)據(jù)庫編碼類型?utf8|utf8mb4|gbk|big5??//?sql為true必傳 ?????*?@param?[type]?string?datauser?????數(shù)據(jù)庫賬號(hào)?//?sql為true必傳 ?????*?@param?[type]?string?datapassword?數(shù)據(jù)庫密碼?//?sql為true必傳 ?????*?@return?false|int ?????*/ ????public?function?AddSite(string?$domain) ????{ ????????$data?=?[ ????????????'webname'?=&gt;?json_encode([ ????????????????'domain'?=&gt;?$domain, ????????????????'domainlist'?=&gt;?[], ????????????????'count'?=&gt;?0 ????????????]), ????????????'path'?=&gt;?config('custom.bt.site_path'), ????????????'type_id'?=&gt;?'0', ????????????'type'?=&gt;?'PHP', ????????????'version'?=&gt;?'74', ????????????'port'?=&gt;?'80', ????????????'ps'?=&gt;?$domain, ????????????'ftp'?=&gt;?'false', ????????????'sql'?=&gt;?'false' ????????]; ????????$response?=?$this-&gt;sendRequest('/site?action=AddSite',?$data); ????????return?(isset($response['siteStatus'])?&amp;&amp;?$response['siteStatus']?===?true)???(int)$response['siteId']?:?false; ????} ????/** ?????*?刪除網(wǎng)站 ?????*?@param?string?$siteName?網(wǎng)站名稱?一般是網(wǎng)站域名 ?????*?@return?bool ?????*/ ????public?function?DeleteSite(string?$siteName):?bool ????{ ????????$site?=?$this-&gt;SiteSearch($siteName); ????????$response?=?$this-&gt;sendRequest('/site?action=DeleteSite',?[ ????????????'id'?=&gt;?$site['id'], ????????????'webname'?=&gt;?$site['name'] ????????]); ????????return?isset($response['status'])?&amp;&amp;?$response['status']?===?true; ????} ????/** ?????*?開啟網(wǎng)站 ?????*?@param?string?$siteName?網(wǎng)站名稱?一般是網(wǎng)站域名 ?????*?@return?bool ?????*/ ????public?function?SiteStart(string?$siteName):?bool ????{ ????????$site?=?$this-&gt;SiteSearch($siteName); ????????$response?=?$this-&gt;sendRequest('/site?action=SiteStart',?[ ????????????'id'?=&gt;?$site['id'], ????????????'name'?=&gt;?$site['name'] ????????]); ????????return?isset($response['status'])?&amp;&amp;?$response['status']?===?true; ????} ????/** ?????*?關(guān)閉網(wǎng)站 ?????*?@param?string?$siteName?網(wǎng)站名稱?一般是網(wǎng)站域名 ?????*?@return?bool ?????*/ ????public?function?SiteStop(string?$siteName):?bool ????{ ????????$site?=?$this-&gt;SiteSearch($siteName); ????????$response?=?$this-&gt;sendRequest('/site?action=SiteStop',?[ ????????????'id'?=&gt;?$site['id'], ????????????'name'?=&gt;?$site['name'] ????????]); ????????return?isset($response['status'])?&amp;&amp;?$response['status']?===?true; ????} ????/** ?????*?為網(wǎng)站綁定域名 ?????*?@param?string?$siteName?網(wǎng)站名稱?一般是網(wǎng)站域名 ?????*?@param?string?$domain?需要綁定的域名 ?????*?@return?bool ?????*/ ????public?function?AddDomain(string?$siteName,?string?$domain) ????{ ????????$site?=?$this-&gt;SiteSearch($siteName); ????????$response?=?$this-&gt;sendRequest('/site?action=AddDomain',?[ ????????????'id'?=&gt;?$site['id'], ????????????'webname'?=&gt;?$site['name'], ????????????'domain'?=&gt;?$domain ????????]); ????????//?綁定成功?status?===?true ????????//?綁定失敗?和?指定域名已綁定過??都返回?status?===?false ????????//?不好區(qū)分?失敗?還是?域名已綁定 ????????return?isset($response['status']); ????} ????/** ?????*?刪除網(wǎng)站綁定的域名 ?????*?@param?string?$siteName?網(wǎng)站名稱?一般是網(wǎng)站域名 ?????*?@param?string?$domain?需要?jiǎng)h除的域名 ?????*?@return?bool ?????*/ ????public?function?DelDomain(string?$siteName,?string?$domain) ????{ ????????$site?=?$this-&gt;SiteSearch($siteName); ????????$response?=?$this-&gt;sendRequest('/site?action=DelDomain',?[ ????????????'id'?=&gt;?$site['id'], ????????????'webname'?=&gt;?$site['name'], ????????????'port'?=&gt;?'80', ????????????'domain'?=&gt;?$domain ????????]); ????????return?isset($response['status'])?&amp;&amp;?$response['status']?===?true; ????} ????/** ?????*?網(wǎng)站設(shè)置SSL證書 ?????*?@param?string?$domain?站點(diǎn)域名 ?????*?@param?string?$key ?????*?@param?string?$csr ?????*?@return?bool ?????*/ ????public?function?SetSSL(string?$domain,?string?$key,?string?$csr):?bool ????{ ????????$data?=?[ ????????????'type'?=&gt;?1, ????????????'siteName'?=&gt;?$domain, ????????????'key'?=&gt;?'', ????????????'csr'?=&gt;?'' ????????]; ????????$response?=?$this-&gt;sendRequest('/site?action=SetSSL',?$data); ????????return?isset($response['status'])?&amp;&amp;?$response['status']?===?true; ????} ????/** ?????*?獲取SSL狀態(tài)及證書詳情 ?????*?@param?string?$domain?站點(diǎn)域名 ?????*?@return?string|false?成功則返回證書到期時(shí)間 ?????*/ ????public?function?GetSSL(string?$domain) ????{ ????????$data?=?[ ????????????'siteName'?=&gt;?$domain ????????]; ????????$response?=?$this-&gt;sendRequest('/site?action=GetSSL',?$data); ????????return?(isset($response['status'])?&amp;&amp;?$response['status']?===?true?&amp;&amp;?$response['cert_data'])???$response['cert_data']['notAfter']?:?false; ????} ????/** ?????*?設(shè)置網(wǎng)站運(yùn)行目錄 ?????*?@param?int?$siteId?站點(diǎn)域名 ?????*?@param?string?$runPath?運(yùn)行目錄路徑 ?????*?@return?bool ?????*/ ????public?function?SetSiteRunPath(int?$siteId,?string?$runPath?=?'/public'):?bool ????{ ????????$data?=?[ ????????????'id'?=&gt;?$siteId, ????????????'runPath'?=&gt;?$runPath ????????]; ????????$response?=?$this-&gt;sendRequest('/site?action=SetSiteRunPath',?$data); ????????return?isset($response['status'])?&amp;&amp;?$response['status']?===?true; ????} ????/** ?????*?獲取網(wǎng)站預(yù)置偽靜態(tài)規(guī)則內(nèi)容(文件內(nèi)容) ?????*?@param?string?$domain?網(wǎng)站域名 ?????*?@param?[type]?$type?0-&gt;獲取內(nèi)置偽靜態(tài)規(guī)則?/www/server/panel/rewrite/nginx/xxxxx.conf;1-&gt;獲取當(dāng)前站點(diǎn)偽靜態(tài)規(guī)則?/www/server/panel/vhost/rewrite/www.baidu.com.conf ?????*?@return?string|false?成功則返回偽靜態(tài)規(guī)則內(nèi)容 ?????*/ ????public?function?GetFileBody(string?$domain) ????{ ????????$data?=?[ ????????????'path'?=&gt;?"/www/server/panel/vhost/rewrite/$domain.conf" ????????]; ????????$response?=?$this-&gt;sendRequest('/files?action=GetFileBody',?$data); ????????return?(isset($response['status'])?&amp;&amp;?$response['status']?===?true)???$response['data']?:?false; ????} ????/** ?????*?保存網(wǎng)站偽靜態(tài)規(guī)則內(nèi)容(保存文件內(nèi)容) ?????*?0-&gt;系統(tǒng)默認(rèn)路徑;1-&gt;自定義全路徑 ?????*?@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'?=&gt;?"/www/server/panel/vhost/rewrite/$domain.conf",?//?偽靜態(tài)文件路徑 ????????????'data'?=&gt;?$htaccess,?//?偽靜態(tài)規(guī)則內(nèi)容?==&gt;?字符串 ????????????'encoding'?=&gt;?'utf-8' ????????]; ????????$response?=?$this-&gt;sendRequest('/files?action=SaveFileBody',?$data); ????????return?isset($response['status'])?&amp;&amp;?$response['status']?===?true; ????} ????/** ?????*?網(wǎng)站申請并設(shè)置SSL證書 ?????*?!!!PS:當(dāng)前請求比較耗時(shí)間?20s-60s不等??最好單獨(dú)使用 ?????*?@param?int?$id?站點(diǎn)ID ?????*?@param?string?$domain?需要申請的域名 ?????*?@return?bool|integer ?????*/ ????public?function?GetApplyCert(int?$id,?string?$domain) ????{ ????????$data?=?[ ????????????"domains"?=&gt;?json_encode([$domain]), ????????????"auth_type"?=&gt;?"http", ????????????"auto_wildcard"?=&gt;?0, ????????????"auth_to"?=&gt;?$id, ????????????"id"?=&gt;?$id, ????????????"siteName"?=&gt;?$domain ????????]; ????????$response?=?$this-&gt;sendRequest('/acme?action=apply_cert_api',?$data); //????????$response?=?[ //????????????'cert'?=&gt;?'', //????????????'root'?=&gt;?'', //????????????'private_key'?=&gt;?'', //????????????'cert_timeout'?=&gt;?1679184499, //????????????'status'?=&gt;?true //????????]; ????????if?(isset($response['status'])?&amp;&amp;?$response['status']?===?true)?{ ????????????Storage::put("ssl/$domain.txt",?json_encode($response)); ????????????$res?=?$this-&gt;SetSSL($domain,?$response['private_key'],?$response['cert']?.?$response['root']); ????????????return?$res???$response['cert_timeout']?:?false; ????????} ????????return?false; ????} }

推薦學(xué)習(xí):《laravel視頻教程》《laravel視頻教程

? 版權(quán)聲明
THE END
喜歡就支持一下吧
點(diǎn)贊8 分享