-2

I have warning with my coding whm plugins

Warning: http_build_query(): Parameter 1 expected to be Array or Object. Incorrect value given in /module_functions.php on line 50

Line 150 is: $query .= '?' . http_build_query($params);

Full line:

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);
}
Hendra Setyawan
  • 75
  • 1
  • 4
  • 12
  • `$params` isn't defined anywhere. Also, `$params` clearly isn't an array or object. You need to figure out *why*. You can start with `var_dump($params);` to see what type it is...if it is even defined at all. – John Conde Dec 29 '18 at 17:00
  • Should $query .= '?' . var_dump($params); ? – Hendra Setyawan Dec 29 '18 at 17:09
  • Wrap it in `If (!empty($params))` or declare the function argument default as `=array()` for simplicity. – mario Dec 29 '18 at 17:25

1 Answers1

0

in the whmapi function, $params has a default value of NULL, and NULL is not a legal argument for http_build_query, and you pass $params straight to http_build_query without first checking if it's null. stop giving http_build_query NULL's, do something like

$query .= '?';
if($params!==NULL){
    $query.=http_build_query($params);
}
hanshenrik
  • 19,904
  • 4
  • 43
  • 89
  • You could also change the definition of the function to set the default value to be an empty array. `Array $params = []` – John Conde Dec 30 '18 at 03:52
  • @JohnConde uhm.. `function f($v=[]){var_dump($v);} f(NULL);` => `NULL` - however, if you're running PHP7+, this would be safe but not accept objects: `function f(array $v=[]){var_dump($v);}`, but if you're running PHP7.1+, this would work and accept objects as well: `function f(iterable $v=[]){var_dump($v);}` – hanshenrik Dec 30 '18 at 12:07