承接 tallesairan/cpanel 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

tallesairan/cpanel

Composer 安装命令:

composer require tallesairan/cpanel

包简介

This class allows developers to execute various functions of the cPanel UAPI and APIv2 directly from their PHP applications

README 文档

README

The cPanel class is a PHP library designed to interact with cPanel APIs, a popular web hosting management platform. This class allows developers to execute various functions of the cPanel UAPI and APIv2 directly from their PHP applications. With the ability to use a proxy and an advanced system of cookies and logs, this class offers enhanced flexibility and security for developers.

This class can help improve productivity in several ways:

  1. Task Automation: The class enables developers to automate cPanel tasks, such as managing email accounts, domains, and databases, directly from their PHP applications.

  2. Application Integration: The class makes it easy to integrate cPanel with custom applications, allowing developers to manage cPanel resources directly from their systems.

  3. Proxy Management: The ability to use a proxy and an advanced system of cookies and logs allows developers to access cPanel from multiple locations and IPs, improving security and flexibility.

  4. Programmatic Control: The class provides a programmatic way to interact with cPanel APIs, allowing developers to create customized and efficient solutions for managing web hosting.

By utilizing the cPanel class, developers can streamline their hosting management operations and improve integration between cPanel and their custom applications, increasing productivity and simplifying the workflow.

Installation via composer

Add it to your project via composer using: composer require tallesairan/cpanel

Examples

  • Simple Login and List files without proxy
<?php
use AiranDev\cPanel;

$ips = [];
$useProxy = false;
try{
    $uapi = new cPanel( $user , $pass, $srv, 2083, true,$useProxy,$ips);

    $allFiles = ($uapi->execute('uapi', 'Fileman', 'list_files',[
            'dirs' => '/public_html',
            'dir' => '/public_html',
            'path' => '/public_html',
            'types'=>'file'
    ]));
    var_dump($allFiles);
        
   }  catch(Exception $e){
         echo $e->getMessage();
         exit;
    }
?>

  • Simple Login and list_files with Proxy:
<?php
use AiranDev\cPanel;
$useProxy = true;
$ips = ["128.0.0.1:3309","8.8.8.8:3309","8.4.4.8:3309"];
try{
    $uapi = new cPanel( $user , $pass, $srv, 2083, true,$useProxy,$ips);

    $allFiles = ($uapi->execute('uapi', 'Fileman', 'list_files',[
            'dirs' => '/public_html',
            'dir' => '/public_html',
            'path' => '/public_html',
            'types'=>'file'
    ]));
    var_dump($allFiles);
        
   }  catch(Exception $e){
         echo $e->getMessage();
         exit;
    }
?>

  • Simple Login and File Upload
<?php
use AiranDev\cPanel;
$useProxy = true;
$ips = ["128.0.0.1:3309","8.8.8.8:3309","8.4.4.8:3309"];
try{
    $uapi = new cPanel( $user , $pass, $srv, 2083, true,$useProxy,$ips);

   $file1 = new CURLFile($indexPath,'text/plain','index.php');
       
    $fileUploadResponse = $uapi->execute('uapi', 'Fileman', 'upload_files', [
            'dir' => 'public_html',
            'overwrite'=>'1',
            'file' => $file1
        ]
    );
    var_dump($fileUploadResponse);
        
   }  catch(Exception $e){
         echo $e->getMessage();
         exit;
    }



  • Simple Login and File Extract
<?php
use AiranDev\cPanel;
$useProxy = true;
$ips = ["128.0.0.1:3309","8.8.8.8:3309","8.4.4.8:3309"];
try{
    $uapi = new cPanel( $user , $pass, $srv, 2083, true,$useProxy,$ips);

   $fileExtractResponse = $uapi->execute('api2','Fileman','fileop',[
        'op'                => 'extract',
        'sourcefiles'       => 'example.zip',
        'destfiles'         => '/public_html',
        'doubledecode'      => '1',
        'overwrite'=>'1' // overwrite if exists
    ]);
    // multiples files extract
    $fileExtractResponse = $uapi->execute('api2','Fileman','fileop',[
        'op'                => 'extract',
        'sourcefiles'       => 'example.zip,example2.zip',
        'destfiles'         => '/public_html,/dir2',
        'doubledecode'      => '1',
        'overwrite'=>'1' // overwrite if exists
    ]);
    var_dump($fileUploadResponse);
        
   }  catch(Exception $e){
         echo $e->getMessage();
         exit;
    }



  • Simple Login and new ftp account
<?php
use AiranDev\cPanel;
$useProxy = true;
$ips = ["128.0.0.1:3309","8.8.8.8:3309","8.4.4.8:3309"];
try{
    $uapi = new cPanel( $user , $pass, $srv, 2083, true,$useProxy,$ips);

    $api2Request = $uapi->execute('uapi','Ftp','add_ftp',[
            'user'=>'user@domain.com',
            'pass'=>'Password@1',
            'homedir'=>'/'
    ]); 
        
   }  catch(Exception $e){
         echo $e->getMessage();
         exit;
    }


  • Create sub domain
<?php
use AiranDev\cPanel;
$useProxy = true;
$ips = ["128.0.0.1:3309","8.8.8.8:3309","8.4.4.8:3309"];
try{
    $uapi = new cPanel( $user , $pass, $srv, 2083, true,$useProxy,$ips);

    // List Domains
    $result = ($uapi->execute('uapi', 'DomainInfo', 'list_domains'));
    // Create Sub-Domain
    $parameters = [
        'domain'                => "demo",
        'rootdomain'            => "example.com",
        'dir'                   => "/home/username/public_html/sub/demo",
        'disallowdot'           => '1',
    ];
    $result = $uapi->execute('api2', 'SubDomain', 'addsubdomain', $parameters);    
   }  catch(Exception $e){
         echo $e->getMessage();
         exit;
    }
  • Manage Email
<?php
use AiranDev\cPanel;
$useProxy = true;
$ips = ["128.0.0.1:3309","8.8.8.8:3309","8.4.4.8:3309"];
try{
    $uapi = new cPanel( $user , $pass, $srv, 2083, true,$useProxy,$ips);

    // List Emails
    $result = ($uapi->execute('uapi', 'Email', 'list_pops'));
    // Add Email
    $parameters = [
            'email' => "demo",
            'password' => "pass01",
            'domain' =>  "demo.com",
            'quota' => "120" //in MB, 0 for unlimited
        ];

    $result = $uapi->execute('uapi', 'Email', 'add_pop', $parameters);

    // Delete Email
    $parameters = [
            'email'           => "demo", // email before @
            'domain'          => "demo.com",
        ];
    $result= ($uapi->execute('uapi', 'Email', 'delete_pop', $parameters));


   }  catch(Exception $e){
         echo $e->getMessage();
         exit;
    }
  • Manage Database
<?php
use AiranDev\cPanel;
$useProxy = true;
$ips = ["128.0.0.1:3309","8.8.8.8:3309","8.4.4.8:3309"];
try{
    $uapi = new cPanel( $user , $pass, $srv, 2083, true,$useProxy,$ips);

    // List Databases (Using API 2)
    $result = $uapi->execute('api2', 'MysqlFE', 'listdbs'); //Response structure is diff. from UAPI response
    // Create Database
    $parameters = [
            'name' => "prefix_database_name",
        ];
    $result = ($uapi->execute('uapi', 'Mysql', 'create_database', $parameters));
    // Delete Database
    $parameters = [
        'name' => "prefix_database_name",
    ];
    $result = ($uapi->execute('uapi', 'Mysql', 'delete_database', $parameters));


   }  catch(Exception $e){
         echo $e->getMessage();
         exit;
    }

Doubts ?

Guide to Replacing cPanel API 1 Functions with UAPI Equivalents

This document lists the UAPI functions which replace previously-deprecated cPanel API 1 functions. We introduced UAPI in cPanel & WHM version 11.42. As we developed additional UAPI functions, we created equivalents to some cPanel API 1 functions. Beginning with cPanel & WHM version 82, we started development for new UAPI modules and functions. These new modules and functions replace any cPanel API 1 functions without a current equivalent.

We are actively removing the cPanel API 1 functions beginning in cPanel & WHM version 88. We strongly recommend that anyone using a cPanel API 1 function use a UAPI function instead.

Replace cPanel API 1 functions with UAPI equivalents

To retrieve a list of cPanel API 1 functions that custom integrations call your system, call the following WHM API 1 functions:

The following examples display code from a cPanel API 1 function and its UAPI equivalent. Click the tab below that corresponds to your chosen development language.

Note:

The examples below compare the cPanel API 1 SetLang::setlang function and the UAPI Locale::set_locale function.

LiveAPI PHP Class

cPanel API 1 function

$cpanel = new CPANEL(); // Connect to cPanel - only do this once.

// Set the "en" locale for the account.
$new_language = $cpanel->api1(
    'SetLang', 'setlang',
        array('en')
);

UAPI Function

$uapi = new cPanel( $user , $pass, $srv, 2083);
# Set the "en" locale for the account.
$setLocale = ($uapi->execute('uapi', 'Locale', 'set_locale',[
        'locale' => 'en',
]));

Note:

For more information, read our Guide to the LiveAPI System.

For more information about how to replace a cPanel API 1 function with a UAPI function, read our Replace a cPanel API 1 Function With a UAPI Function documentation.

UAPI functions and legacy equivalents

The following tables list the UAPI functions that we added starting in cPanel & WHM version 82. The tables also list their cPanel API 1 equivalent functions.

For a complete list of UAPI functions and cPanel API 1 functions, read our Guide to UAPI and Guide to cPanel API 1 documentation.

Fileman

cPanel API 1 Functions UAPI Functions
Fileman::restoredb Backup::restore_databases
Fileman::restorefiles Backup::restore_files
Fileman::restoreaf

MySQL®

cPanel API 1 Functions UAPI Functions
Mysql::listdbs Mysql::listdbsopt
Mysql::number_of_dbs Mysql::list_databases
Mysql::routines Mysql::list_routines
Mysql::listusers Mysql::listusersopt
Mysql::number_of_users Mysql::list_users
Mysql::updateprivs Mysql::update_privileges

Example For using uapi from cpanel documentation

cPanel API 1 function

$cpanel = new CPANEL(); // Connect to cPanel - only do this once.

// Set the "en" locale for the account.
$set_locale = $cpanel->uapi(
    'Locale', 'set_locale',
    array(
        'locale' => 'en',
    )
);

UAPI Function

$uapi = new cPanel( $user , $pass, $srv, 2083);
# Set the "en" locale for the account.
$setLocale = ($uapi->execute('uapi', 'Locale', 'set_locale',[
        'locale' => 'en',
]));

For more read at Guide to Replacing cPanel API 1 Functions with UAPI Equivalents

UAPI Docs UAPI Complete Docs

API

Description

The cPanel class provides an interface to interact with cPanel's UAPI and APIv2. It supports using proxies, and has an advanced system for handling cookies and logs.

Class Properties

$host

  • Type: string
  • The cPanel host URL.

$port

  • Type: integer
  • The cPanel host port.

$username

  • Type: string
  • The cPanel account username.

$password

  • Type: string
  • The cPanel account password.

$log

  • Type: boolean
  • Indicates whether logging is enabled.

$cFile

  • Type: string
  • The cookie file path.

$curlfile

  • Type: string
  • The log file path.

$emailArray, $cpsess, $homepage, $exPage

  • These private properties are not currently used in the class.

$ips

  • Type: array
  • An array of proxy IPs.

$useragent

  • Type: string
  • The user agent string to use for HTTP requests.

$current_ip

  • Type: string
  • The current IP address for the session.

$useProxy

  • Type: boolean
  • Indicates whether to use a proxy or not.

Class Methods

__construct

  • Parameters:
    • string $username
    • string $password
    • string $host
    • integer $port (default: 2083)
    • boolean $log (default: false)
    • boolean $useProxy (default: false)
    • array $proxies (default: [])
  • Initializes the cPanel class with the provided parameters.

getRandomProxy

  • Return: string
  • Returns a random proxy from the list of proxies.

Request

  • Parameters:
    • [type] $url
    • array $params (default: [])
  • Return: mixed
  • Description: Handles an HTTP request to the cPanel server. If $params is set, it will be a POST request. This method is private and used internally within the class.

signIn

  • Parameters: None
  • Return: void
  • Description: Starts a session at the cPanel server. This method is private and called during the initialization of the cPanel class. It sets the $cpsess, $homepage, and $exPage properties of the class based on the response from the cPanel server.

execute

  • Parameters:
    • string $api
    • string $module
    • string $function
    • array $parameters (default: [])
  • Return: void
  • Description: Executes the specified function using the given API type (api2 or uapi), module, and function, with the provided parameters. Calls either the api2 or uapi method based on the API type.

uapi

  • Parameters:
    • [type] $module
    • [type] $function
    • array $parameters (default: [])
  • Return: void
  • Description: Executes a UAPI function with the given module, function, and parameters. If the function is 'upload_files', the files are sent as an array of parameters. Returns a JSON decoded response from the cPanel server.

api2

  • Parameters:
    • string $module
    • string $function
    • array $parameters (default: [])
  • Return: string
  • Description: Executes an APIv2 function with the provided module, function, and parameters. Builds the APIv2 request URL and returns a JSON decoded response from the cPanel server.

slugify

  • Parameters:
    • string $text
  • Return: void
  • Description: A simple function to convert a given string into a slug (a URL-friendly version of the string). The function replaces non-letter or non-digit characters with hyphens, transliterates the text to ASCII, removes unwanted characters, trims hyphens from the beginning and end of the string, and converts the text to lowercase. If the resulting slug is empty, the function returns 'n-a'.

How to contribute

  • Create a fork, make changes and send a pull request.
  • Raise a issue

RoadMap

  • getSessionUrl() => Returns the current logged in session url

Based On

Built from an abandoned project myPHPnotes/Cpanel

License

Licensed under Apache 2.0. You can check its details here.

tallesairan/cpanel 适用场景与选型建议

tallesairan/cpanel 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 135 次下载、GitHub Stars 达 2, 最近一次更新时间为 2023 年 05 月 09 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「http」 「rest」 「api」 「cpanel」 「UAPI」 「api2」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 tallesairan/cpanel 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 tallesairan/cpanel 我们能提供哪些服务?
定制开发 / 二次开发

基于 tallesairan/cpanel 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 135
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 21
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-05-09