今天小编给大家分享一下Laravel如何操作宝塔面板API的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。
Laravel 操作宝塔面板 API
代码如下:
<?phpnamespace App\Http\Controllers\Custom;use App\Http\Controllers\Controller;use Illuminate\Support\Facades\Http;class BtPanel extends Controller{ 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; } 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']) ]; } 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; } 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; } 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; } 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; } 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']); } 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; } 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; } 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; } 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; } 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; } 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; } 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如何操作宝塔面板API”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注编程网行业资讯频道。