我有此代码:
include "xmlapi.php";
$pass = 'testing1234';
$auser = 'testing';
$server = "0.0.0.0";
$port = 2087;
$remote_api = new xmlapi($server);
$remote_api->password_auth($auser, $pass);
$remote_api->set_port($port);
$remote_api->set_output('json');
$json_list = $remote_api->xmlapi_query('listaccts', array( 'api.version'=> 1));
$list = json_decode($json_list, true);
if ( ! is_array($list)
|| ! array_key_exists('data', $list)
|| ! is_array($list['data'])
|| ! array_key_exists('acct', $list['data'])
) {
die("Invalid response!");
}
$email_list = array();
foreach ($list['data']['acct'] as $acct) {
$username = $acct['user'];
$json_emails = $remote_api->api2_query($username, 'Email', 'listpopswithdisk', array());
$acct_emails = json_decode($json_emails, true);
if ( is_array($acct_emails)
&& array_key_exists('cpanelresult', $acct_emails)
&& is_array($acct_emails['cpanelresult'])
&& array_key_exists('data', $acct_emails['cpanelresult'])
&& is_array($acct_emails['cpanelresult']['data'])
) {
foreach ($acct_emails['cpanelresult']['data'] as $an_email) {
array_push(
$email_list,
array(
'cpanel_account' => $username,
'domain' => $an_email['domain'],
'email' => $an_email['user'],
'full_email' => $an_email['email'],
'diskused' => $an_email['diskused'],
)
);
echo $an_email['email'] . ' - ';
echo $an_email['diskused'] . ' - ';
echo $an_email['user'] . '<br>';
}
}
}
我使用此代码连接到cpanel并列出了服务器上的所有电子邮件。 首先,我只是使用var_dump来查看它是否有效。并尝试使用foreach循环,但未成功。 我该如何执行循环以回显所有数据,以便可以将数据放入表或类似的表中。
请帮助我,谢谢。
答案 0 :(得分:0)
您需要解析包含电子邮件帐户的数组并打印到<table>
尝试修改此代码:
$email_list = [
['email' => 'first@email.com', 'diskused' => 'the diskused', 'user' => 'the first user'],
['email' => 'second@email.com', 'diskused' => 'the diskused', 'user' => 'the second user'],
['email' => 'third@email.com', 'diskused' => 'the diskused', 'user' => 'the third user'],
];
if (count($email_list)) {
print '<table>';
/* Set the header fields to print and Titles */
$headers = [
'email' => 'Email',
'diskused' => 'Diskused',
'user' => 'User'
];
/* Print table headers */
print '<tr>';
foreach ($headers as $headerTitle) {
print '<th>'.$headerTitle.'</th>';
}
print '</tr>';
/* Parse all rows */
foreach ($email_list as $row) {
print '<tr>';
/* Print all values */
foreach (array_keys($headers) as $headerKey) {
print '<td>'.$row[$headerKey].'</td>';
}
print '</tr>';
}
print '</table>';
}