我编写了以下函数,该函数返回密钥的值,该密钥是cPanel帐户的帐户前缀。 [prefix] ='oneclick _'
该函数有效,但我只是一个初学者,我敢肯定有一种更简便,更好的方法来编写此函数。
<?php
// Get data from cPanel API
$array = $cp_db_restrictions['cpanelresult']['result']['data'];
//Pass array of $data to function
db_prefix( $array );
//Function accepts array of $data
function db_prefix( $array ) {
if( count( $array ) > 0 ) {
return $array['prefix'];
} else {
return "No prefix";
}
}
?>
<?php echo db_prefix($array); ?>
该函数回显“ oneclick _”
编辑: 这是返回数组的代码:
$cp_db_restrictions = $cpanel->uapi(
'Mysql','get_restrictions'
);
但是,我不太确定如何将其添加到函数中。当我尝试时,出现以下错误消息:
注意:未定义的变量:...中的数组
致命错误:未捕获的错误:在...中调用null的成员函数uapi()
解决方案*
因此,感谢尼克的回答,这就是解决方案。您必须确保在文档顶部和相同的PHP标签中声明cPanel类。
<?php
include("/usr/local/cpanel/php/cpanel.php"); // Instantiate the CPANEL object.
$cpanel = new CPANEL();
function db_prefix($cpanel) {
$cp_db_restrictions = $cpanel->uapi('Mysql','get_restrictions');
$array = $cp_db_restrictions['cpanelresult']['result']['data'];
return $array['prefix'] ?? 'No prefix';
} ?>
<?php echo db_prefix($cpanel); ?>