-2

我的编码 whm 插件有警告

警告:http_build_query():参数 1 应为数组或对象。第 50 行 /module_functions.php 中给出的值不正确

第 150 行是:$query .= '?' . http_build_query($params);

全线:

public function whmaapicall()
{
    $whmusername = $_ENV['REMOTE_USER'];
    $whmpassword = $_ENV['REMOTE_PASSWORD'];
    $query = 'https://127.0.0.1:2087/json-api/listpkgs?api.version=1';
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $header[0] = 'Authorization: Basic ' . base64_encode($whmusername . ':' . $whmpassword) . "\n\r";
    curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
    curl_setopt($curl, CURLOPT_URL, $query);
    $result = curl_exec($curl);

    if (!$result) {
        error_log('curl_exec threw error "' . curl_error($curl) . '" for ' . $query);
    }

    curl_close($curl);
    return json_decode($result);
}

public function whmapi($function = NULL, $params = NULL)
{
    $whmusername = 'root';

    if ($function == 'listpkgs') {
        $whmusername = $_ENV['REMOTE_USER'];
        return $this->whmapi2();
    }

    $whmhash = $this->gethash();
    $query = 'https://127.0.0.1:2087/json-api/' . $function;
    $query .= '?' . http_build_query($params); //line mentioned
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $header[0] = 'Authorization: WHM ' . $whmusername . ':' . preg_replace('\'(' . "\r" . '|' . "\n" . ')\'', '', $whmhash);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
    curl_setopt($curl, CURLOPT_URL, $query);
    $result = curl_exec($curl);
    curl_close($curl);
    return json_decode($result);
}
4

1 回答 1

0

在 whmapi 函数中,$params 的默认值为 NULL,而 NULL 不是 http_build_query 的合法参数,并且您将 $params 直接传递给 http_build_query 而不首先检查它是否为空。停止给 http_build_query NULL,做类似的事情

$query .= '?';
if($params!==NULL){
    $query.=http_build_query($params);
}
于 2018-12-30T00:13:32.867 回答