Skip to content

Instantly share code, notes, and snippets.

@sendyputra
Created May 2, 2016 13:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sendyputra/4e505f9730188db1e8b0a5709c82510d to your computer and use it in GitHub Desktop.
Save sendyputra/4e505f9730188db1e8b0a5709c82510d to your computer and use it in GitHub Desktop.
function whmAPI($server_ip, $root_pass, $function, $parameter = '', $type='')
{
$whmusername = "root";
if ($type == 'cpanel') {
$query = "https://" . $server_ip . ":2087/json-api/cpanel?api.version=1&cpanel_jsonapi_apiversion=2" . $parameter;
} else {
$query = "https://" . $server_ip . ":2087/json-api/" . $function . "?api.version=1" . $parameter;
}
$curl = curl_init(); // Create Curl Object
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // Allow self-signed certs
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); // Allow certs that do not match the hostname
curl_setopt($curl, CURLOPT_HEADER, 0); // Do not include header in output
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // Return contents of transfer on curl_exec
$header[ 0 ] = "Authorization: Basic " . base64_encode($whmusername . ":" . $root_pass) . "\n\r";
curl_setopt($curl, CURLOPT_HTTPHEADER, $header); // set the username and password
curl_setopt($curl, CURLOPT_URL, $query); // execute the query
$result = curl_exec($curl);
if ($result == false) {
error_log("curl_exec threw error \"" . curl_error($curl) . "\" for $query");
// log error if curl exec fails
}
curl_close($curl);
$result = json_decode($result, true);
$status = $result[ 'metadata' ][ 'result' ];
$reason = $result[ 'metadata' ][ 'reason' ];
$response = array(
'status' => $status,
'reason' => $reason,
'result' => $result
);
return $response;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment