使用 PHP 创建 WHM 帐户列表

Use PHP to create list of WHM accounts

我想知道是否可以使用 cPanel PublicAPI PHP 存储库 https://github.com/CpanelInc/publicapi-php 通过 PHP 连接到我的 WHM这样我就可以在 WHM 中创建帐户列表。

有什么我应该知道的吗?有什么限制吗?我只是想填充我的 WHM 中的帐户列表。

现在我收到一个错误

Warning: count(): Parameter must be an array or an object that implements Countable in

我在想是不是有什么东西挡住了我?

我的代码是这样的,我想创建一个 php 变量来保存数组数据,然后遍历它打印帐户域名

require_once '../_libraries/publicapi-php-master/Cpanel/Util/Autoload.php';
$config = array(
    'service' => array(
        'whm' => array(
            'config'    => array(
                'host' => 'XXXXXXXXXXXXXX',
                'user' => 'XXXXXXXXXXXXXX',
                'password' => 'XXXXXXXXXX'
            ),
        ),
    ),
);
$cp = Cpanel_PublicAPI::getInstance($config);
$accounts = $cp->whm_api('listaccts');
print_R($accounts);
#print $accounts->_response->dataContainer->storage->acct->dataContainer->storage[0];

当我执行 print_r($accounts) 这就是我得到的,我只需要知道如何遍历它并使用 PHP 循环遍历它。我可以在此输出中看到第一个域是 cfpacking.com,这就是我要查找的数据。

Cpanel_Query_Object Object
(
    [_query:Cpanel_Query_Object:private] => Cpanel_Core_Object Object
        (
            [dataContainer:protected] => ArrayObject Object
                (
                    [storage:ArrayObject:private] => Array
                        (
                            [adapter] => whostmgr
                            [client] => curl
                            [url] => https://XXXXXXXXXXXXXX:2087/json-api/listaccts
                            [args] => 
                            [argsArray] => Cpanel_Core_Object Object
                                (
                                    [dataContainer:protected] => ArrayObject Object
                                        (
                                            [storage:ArrayObject:private] => Array
                                                (
                                                )

                                        )

                                )

                            [authstr] => Authorization: Basic XXXXXXXXXXXXXXXXXXXXXXX==

                            [directURL] => 
                        )

                )

        )

    [_response:Cpanel_Query_Object:private] => Cpanel_Core_Object Object
        (
            [dataContainer:protected] => ArrayObject Object
                (
                    [storage:ArrayObject:private] => Array
                        (
                            [status] => 1
                            [statusmsg] => Ok
                            [acct] => Cpanel_Core_Object Object
                                (
                                    [dataContainer:protected] => ArrayObject Object
                                        (
                                            [storage:ArrayObject:private] => Array
                                                (
                                                    [0] => Cpanel_Core_Object Object
                                                        (
                                                            [dataContainer:protected] => ArrayObject Object
                                                                (
                                                                    [storage:ArrayObject:private] => Array
                                                                        (
                                                                            [inodeslimit] => 500000
                                                                            [ip] => XXXXXXXXXXXXXXX
                                                                            [mailbox_format] => maildir
                                                                            [plan] => default
                                                                            [maxftp] => unlimited
                                                                            [maxparked] => unlimited
                                                                            [owner] => XXXXXXXXXXXXXXXXXXX
                                                                            [maxpop] => unlimited
                                                                            [email] => XXXXXXXXXXXXXXXXXXXXXX
                                                                            [max_email_per_hour] => 500
                                                                            [disklimit] => unlimited
                                                                            [maxlst] => unlimited
                                                                            [min_defer_fail_to_trigger_protection] => 5
                                                                            [backup] => 1
                                                                            [startdate] => 17 Dec 27 15:59
                                                                            [inodesused] => 3281
                                                                            [maxsql] => unlimited
                                                                            [max_defer_fail_percentage] => 25
                                                                            [ipv6] => Cpanel_Core_Object Object
                                                                                (
                                                                                    [dataContainer:protected] => ArrayObject Object
                                                                                        (
                                                                                            [storage:ArrayObject:private] => Array
                                                                                                (
                                                                                                )

                                                                                        )

                                                                                )

                                                                            [max_emailacct_quota] => unlimited
                                                                            [maxsub] => unlimited
                                                                            [unix_startdate] => 1514411971
                                                                            [outgoing_mail_hold] => 0
                                                                            [partition] => home
                                                                            [legacy_backup] => 1
                                                                            [maxaddons] => unlimited
                                                                            [suspendtime] => 0
                                                                            [uid] => 1750
                                                                            [suspendreason] => not suspended
                                                                            [suspended] => 0
                                                                            [user] => XXXXXXXXXXXXXX
                                                                            [domain] => cfpacking.com
                                                                            [diskused] => 79M
                                                                            [theme] => paper_lantern
                                                                            [temporary] => 0
                                                                            [is_locked] => 0
                                                                            [shell] => /bin/bash
                                                                            [outgoing_mail_suspended] => 0
                                                                        )

                                                                )

                                                        )

                                                    [1] => Cpanel_Core_Object Object

可以使用以下语法将响应对象转换为数组:

require_once '../_libraries/publicapi-php-master/Cpanel/Util/Autoload.php';
$config = array(
    'service' => array(
        'whm' => array(
            'config'    => array(
                'host' => 'XXXXXXXXXXXXXXXXXXXX',
                'user' => 'XXXXXXXXXXXXXXXXXXXX',
                'password' => 'XXXXXXXXXXXXXXXX'
            ),
        ),
    ),
);
$cp = Cpanel_PublicAPI::getInstance($config);
$accounts = $cp->whm_api('listaccts',array('search'=>'XXXXXXXXX','searchtype'=>'owner'));
$accounts = $accounts->getResponse('array')['acct'];

foreach($accounts as $account){
    print $account['domain'];
    print '<br />';
}

库(https://github.com/CpanelInc/publicapi-php) contains code that generates warnings in php 7.2 because of a breaking change。因为库已经9年没维护了,你得自己修了。

修复: 旧:

Cpanel/PublicApi.php line 343

if (count($storedServicesConfig) > 1) {

新:

Cpanel/PublicApi.php line 343

if (count($storedServicesConfig->getAllData()) > 1) {