首页 > 解决方案 > 使用 PHP 创建 WHM 帐户列表

问题描述

我想知道是否可以使用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

标签: phpaccountwhm

解决方案


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

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 ) 包含由于重大更改而在 php 7.2 中生成警告的代码。因为图书馆已经 9 年没有维护了,所以你必须自己修复它。

修复:旧:

Cpanel/PublicApi.php 第 343 行

if (count($storedServicesConfig) > 1) {

新的:

Cpanel/PublicApi.php 第 343 行

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

推荐阅读