This article is provided by the Laravel Tutorial column to introduce you to the relevant knowledge of the Laravel Pagoda Panel. It mainly shares with you how Laravel operates the Pagoda Panel API. Let's take a look at it. I hope it will be helpful to friends who need it!
Laravel operation pagoda panel API
Modify different places according to your own business! ! !
For other interfaces, please check the official documentation: https://www.bt.cn/api-doc.pdf.
The code is as follows:
<?php namespace 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; } }
Recommended learning: "laravel video tutorial" "Pagoda usage tutorial"
The above is the detailed content of Share how Laravel operates the Pagoda Panel API. For more information, please follow other related articles on the PHP Chinese website!

What new features and best practices does Laravel's migration system offer in the latest version? 1. Added nullableMorphs() for polymorphic relationships. 2. The after() method is introduced to specify the column order. 3. Emphasize handling of foreign key constraints to avoid orphaned records. 4. It is recommended to optimize performance, such as adding indexes appropriately. 5. Advocate the idempotence of migration and the use of descriptive names.

Laravel10,releasedinFebruary2023,isthelatestLTSversion,supportedforthreeyears.ItrequiresPHP8.1 ,enhancesLaravelPennantforfeatureflags,improveserrorhandling,refinesdocumentation,andoptimizesperformance,particularlyinEloquentORM.

Laravel's latest version introduces multiple new features: 1. LaravelPennant is used to manage function flags, allowing new features to be released in stages; 2. LaravelReverb simplifies the implementation of real-time functions, such as real-time comments; 3. LaravelVite accelerates the front-end construction process; 4. The new model factory system enhances the creation of test data; 5. Improves the error handling mechanism and provides more flexible error page customization options.

Softleteinelelavelisling -Memptry-braceChortsDevetus -TeedeecetovedinglyDeveledTeecetteecedelave

Laravel10.xisthecurrentversion,offeringnewfeatureslikeenumsupportinEloquentmodelsandimprovedroutemodelbindingwithenums.Theseupdatesenhancecodereadabilityandsecurity,butrequirecarefulplanningandincrementalimplementationforasuccessfulupgrade.

LaravelmigrationsstreamlinedatabasemanagementbyallowingschemachangestobedefinedinPHPcode,whichcanbeversion-controlledandshared.Here'showtousethem:1)Createmigrationclassestodefineoperationslikecreatingormodifyingtables.2)Usethe'phpartisanmigrate'comma

To find the latest version of Laravel, you can visit the official website laravel.com and click the "Docs" button in the upper right corner, or use the Composer command "composershowlaravel/framework|grepversions". Staying updated can help improve project security and performance, but the impact on existing projects needs to be considered.

YoushouldupdatetothelatestLaravelversionforperformanceimprovements,enhancedsecurity,newfeatures,bettercommunitysupport,andlong-termmaintenance.1)Performance:Laravel9'sEloquentORMoptimizationsenhanceapplicationspeed.2)Security:Laravel8introducedbetter


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version
Chinese version, very easy to use

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver Mac version
Visual web development tools
