Home PHP Framework Laravel Share how Laravel operates the Pagoda Panel API

Share how Laravel operates the Pagoda Panel API

Dec 29, 2022 pm 05:05 PM
laravel pagoda panel

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!

Share how Laravel operates the Pagoda Panel API

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(&#39;custom.bt.key&#39;);
        // 宝塔面板地址 http://xxx.xxx.xxx:2222 填写至端口即可
        $panelPath = config(&#39;custom.bt.panel_path&#39;);
        $time = time();
        $response = Http::withOptions([&#39;verify&#39; => false])
            ->retry(2, 5000) // !!!这里时间不适用于 GetApplyCert 接口
            ->attach(&#39;cookie&#39;, $secretKey, &#39;bt.cookie&#39;) // 随便传东西就行
            ->post($panelPath . $path, array_merge([
                &#39;request_token&#39; => md5($time . &#39;&#39; . md5($secretKey)),
                &#39;request_time&#39; => $time
            ], $query))
            ->json();
        return $response ?: false;
    }
    /**
     * 查询网站
     * @param string|null $search 需要搜索的关键词
     * @return array|false
     */
    public function SiteSearch(string $search = null)
    {
        $search = $search ?: config(&#39;custom.bt.domain&#39;);
        $response = $this->sendRequest(&#39;/data?action=getData&table=sites&#39;, [
            &#39;limit&#39; => 5,
            &#39;search&#39; => $search
        ]);
        // 获取失败
        if (!isset($response[&#39;data&#39;])) return false;
        // 不允许出现相似的网站名
        if (count($response[&#39;data&#39;]) != 1) return false;
        $site = $response[&#39;data&#39;][0];
        return [
            &#39;id&#39; => $site[&#39;id&#39;],
            &#39;name&#39; => $site[&#39;name&#39;],
            &#39;path&#39; => $site[&#39;path&#39;],
            &#39;ps&#39; => $site[&#39;ps&#39;],
            &#39;php&#39; => str_replace(&#39;.&#39;, &#39;&#39;, $site[&#39;php_version&#39;])
        ];
    }
    /**
     * 创建网站
     * !!!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 = [
            &#39;webname&#39; => json_encode([
                &#39;domain&#39; => $domain,
                &#39;domainlist&#39; => [],
                &#39;count&#39; => 0
            ]),
            &#39;path&#39; => config(&#39;custom.bt.site_path&#39;),
            &#39;type_id&#39; => &#39;0&#39;,
            &#39;type&#39; => &#39;PHP&#39;,
            &#39;version&#39; => &#39;74&#39;,
            &#39;port&#39; => &#39;80&#39;,
            &#39;ps&#39; => $domain,
            &#39;ftp&#39; => &#39;false&#39;,
            &#39;sql&#39; => &#39;false&#39;
        ];
        $response = $this->sendRequest(&#39;/site?action=AddSite&#39;, $data);
        return (isset($response[&#39;siteStatus&#39;]) && $response[&#39;siteStatus&#39;] === true) ? (int)$response[&#39;siteId&#39;] : false;
    }
    /**
     * 删除网站
     * @param string $siteName 网站名称 一般是网站域名
     * @return bool
     */
    public function DeleteSite(string $siteName): bool
    {
        $site = $this->SiteSearch($siteName);
        $response = $this->sendRequest(&#39;/site?action=DeleteSite&#39;, [
            &#39;id&#39; => $site[&#39;id&#39;],
            &#39;webname&#39; => $site[&#39;name&#39;]
        ]);
        return isset($response[&#39;status&#39;]) && $response[&#39;status&#39;] === true;
    }
    /**
     * 开启网站
     * @param string $siteName 网站名称 一般是网站域名
     * @return bool
     */
    public function SiteStart(string $siteName): bool
    {
        $site = $this->SiteSearch($siteName);
        $response = $this->sendRequest(&#39;/site?action=SiteStart&#39;, [
            &#39;id&#39; => $site[&#39;id&#39;],
            &#39;name&#39; => $site[&#39;name&#39;]
        ]);
        return isset($response[&#39;status&#39;]) && $response[&#39;status&#39;] === true;
    }
    /**
     * 关闭网站
     * @param string $siteName 网站名称 一般是网站域名
     * @return bool
     */
    public function SiteStop(string $siteName): bool
    {
        $site = $this->SiteSearch($siteName);
        $response = $this->sendRequest(&#39;/site?action=SiteStop&#39;, [
            &#39;id&#39; => $site[&#39;id&#39;],
            &#39;name&#39; => $site[&#39;name&#39;]
        ]);
        return isset($response[&#39;status&#39;]) && $response[&#39;status&#39;] === true;
    }
    /**
     * 为网站绑定域名
     * @param string $siteName 网站名称 一般是网站域名
     * @param string $domain 需要绑定的域名
     * @return bool
     */
    public function AddDomain(string $siteName, string $domain)
    {
        $site = $this->SiteSearch($siteName);
        $response = $this->sendRequest(&#39;/site?action=AddDomain&#39;, [
            &#39;id&#39; => $site[&#39;id&#39;],
            &#39;webname&#39; => $site[&#39;name&#39;],
            &#39;domain&#39; => $domain
        ]);
        // 绑定成功 status === true
        // 绑定失败 和 指定域名已绑定过  都返回 status === false
        // 不好区分 失败 还是 域名已绑定
        return isset($response[&#39;status&#39;]);
    }
    /**
     * 删除网站绑定的域名
     * @param string $siteName 网站名称 一般是网站域名
     * @param string $domain 需要删除的域名
     * @return bool
     */
    public function DelDomain(string $siteName, string $domain)
    {
        $site = $this->SiteSearch($siteName);
        $response = $this->sendRequest(&#39;/site?action=DelDomain&#39;, [
            &#39;id&#39; => $site[&#39;id&#39;],
            &#39;webname&#39; => $site[&#39;name&#39;],
            &#39;port&#39; => &#39;80&#39;,
            &#39;domain&#39; => $domain
        ]);
        return isset($response[&#39;status&#39;]) && $response[&#39;status&#39;] === true;
    }
    /**
     * 网站设置SSL证书
     * @param string $domain 站点域名
     * @param string $key
     * @param string $csr
     * @return bool
     */
    public function SetSSL(string $domain, string $key, string $csr): bool
    {
        $data = [
            &#39;type&#39; => 1,
            &#39;siteName&#39; => $domain,
            &#39;key&#39; => &#39;&#39;,
            &#39;csr&#39; => &#39;&#39;
        ];
        $response = $this->sendRequest(&#39;/site?action=SetSSL&#39;, $data);
        return isset($response[&#39;status&#39;]) && $response[&#39;status&#39;] === true;
    }
    /**
     * 获取SSL状态及证书详情
     * @param string $domain 站点域名
     * @return string|false 成功则返回证书到期时间
     */
    public function GetSSL(string $domain)
    {
        $data = [
            &#39;siteName&#39; => $domain
        ];
        $response = $this->sendRequest(&#39;/site?action=GetSSL&#39;, $data);
        return (isset($response[&#39;status&#39;]) && $response[&#39;status&#39;] === true && $response[&#39;cert_data&#39;]) ? $response[&#39;cert_data&#39;][&#39;notAfter&#39;] : false;
    }
    /**
     * 设置网站运行目录
     * @param int $siteId 站点域名
     * @param string $runPath 运行目录路径
     * @return bool
     */
    public function SetSiteRunPath(int $siteId, string $runPath = &#39;/public&#39;): bool
    {
        $data = [
            &#39;id&#39; => $siteId,
            &#39;runPath&#39; => $runPath
        ];
        $response = $this->sendRequest(&#39;/site?action=SetSiteRunPath&#39;, $data);
        return isset($response[&#39;status&#39;]) && $response[&#39;status&#39;] === 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 = [
            &#39;path&#39; => "/www/server/panel/vhost/rewrite/$domain.conf"
        ];
        $response = $this->sendRequest(&#39;/files?action=GetFileBody&#39;, $data);
        return (isset($response[&#39;status&#39;]) && $response[&#39;status&#39;] === true) ? $response[&#39;data&#39;] : false;
    }
    /**
     * 保存网站伪静态规则内容(保存文件内容)
     * 0->系统默认路径;1->自定义全路径
     * @param string $domain
     * @param string|null $htaccess
     * @return bool
     */
    public function SaveFileBody(string $domain, string $htaccess = null): bool
    {
        $htaccess = $htaccess ?: config(&#39;custom.bt.htaccess&#39;);
        $data = [
            &#39;path&#39; => "/www/server/panel/vhost/rewrite/$domain.conf", // 伪静态文件路径
            &#39;data&#39; => $htaccess, // 伪静态规则内容 ==> 字符串
            &#39;encoding&#39; => &#39;utf-8&#39;
        ];
        $response = $this->sendRequest(&#39;/files?action=SaveFileBody&#39;, $data);
        return isset($response[&#39;status&#39;]) && $response[&#39;status&#39;] === 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(&#39;/acme?action=apply_cert_api&#39;, $data);
//        $response = [
//            &#39;cert&#39; => &#39;&#39;,
//            &#39;root&#39; => &#39;&#39;,
//            &#39;private_key&#39; => &#39;&#39;,
//            &#39;cert_timeout&#39; => 1679184499,
//            &#39;status&#39; => true
//        ];
        if (isset($response[&#39;status&#39;]) && $response[&#39;status&#39;] === true) {
            Storage::put("ssl/$domain.txt", json_encode($response));
            $res = $this->SetSSL($domain, $response[&#39;private_key&#39;], $response[&#39;cert&#39;] . $response[&#39;root&#39;]);
            return $res ? $response[&#39;cert_timeout&#39;] : false;
        }
        return false;
    }
}
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1669
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

Solve caching issues in Craft CMS: Using wiejeben/craft-laravel-mix plug-in Solve caching issues in Craft CMS: Using wiejeben/craft-laravel-mix plug-in Apr 18, 2025 am 09:24 AM

When developing websites using CraftCMS, you often encounter resource file caching problems, especially when you frequently update CSS and JavaScript files, old versions of files may still be cached by the browser, causing users to not see the latest changes in time. This problem not only affects the user experience, but also increases the difficulty of development and debugging. Recently, I encountered similar troubles in my project, and after some exploration, I found the plugin wiejeben/craft-laravel-mix, which perfectly solved my caching problem.

How to learn Laravel How to learn Laravel for free How to learn Laravel How to learn Laravel for free Apr 18, 2025 pm 12:51 PM

Want to learn the Laravel framework, but suffer from no resources or economic pressure? This article provides you with free learning of Laravel, teaching you how to use resources such as online platforms, documents and community forums to lay a solid foundation for your PHP development journey from getting started to master.

Laravel user login function Laravel user login function Apr 18, 2025 pm 12:48 PM

Laravel provides a comprehensive Auth framework for implementing user login functions, including: Defining user models (Eloquent model), creating login forms (Blade template engine), writing login controllers (inheriting Auth\LoginController), verifying login requests (Auth::attempt) Redirecting after login is successful (redirect) considering security factors: hash passwords, anti-CSRF protection, rate limiting and security headers. In addition, the Auth framework also provides functions such as resetting passwords, registering and verifying emails. For details, please refer to the Laravel documentation: https://laravel.com/doc

What versions of laravel are there? How to choose the version of laravel for beginners What versions of laravel are there? How to choose the version of laravel for beginners Apr 18, 2025 pm 01:03 PM

In the Laravel framework version selection guide for beginners, this article dives into the version differences of Laravel, designed to assist beginners in making informed choices among many versions. We will focus on the key features of each release, compare their pros and cons, and provide useful advice to help beginners choose the most suitable version of Laravel based on their skill level and project requirements. For beginners, choosing a suitable version of Laravel is crucial because it can significantly impact their learning curve and overall development experience.

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

How to view the version number of laravel? How to view the version number of laravel How to view the version number of laravel? How to view the version number of laravel Apr 18, 2025 pm 01:00 PM

The Laravel framework has built-in methods to easily view its version number to meet the different needs of developers. This article will explore these methods, including using the Composer command line tool, accessing .env files, or obtaining version information through PHP code. These methods are essential for maintaining and managing versioning of Laravel applications.

The difference between laravel and thinkphp The difference between laravel and thinkphp Apr 18, 2025 pm 01:09 PM

Laravel and ThinkPHP are both popular PHP frameworks and have their own advantages and disadvantages in development. This article will compare the two in depth, highlighting their architecture, features, and performance differences to help developers make informed choices based on their specific project needs.

See all articles