Last active
December 26, 2019 15:43
-
-
Save belfie13/8ae3ba006d571c48e3a1e9b30bd97529 to your computer and use it in GitHub Desktop.
cPanel UAPI HTTPS Remote Procedure Call with cURL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* takes an array of parameters and formats them into an array for UAPI RPC. | |
* $columns = [ | |
* 'value', | |
* 'value' | |
* ]; | |
*/ | |
function get_columns_parameters(array $columns = null):array | |
{ | |
if (!$columns) | |
return ['api.columns' => 0]; | |
$params = ['api.columns' => 1]; | |
$index = 0; | |
foreach ($columns as $value) | |
{ | |
$params['api.columns_'.$index] = $value; | |
$index++; | |
} | |
return $params; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* takes an array of parameters and formats them into an array for UAPI RPC. | |
* $filters = [ | |
* ['key' => $key, 'value' => $value, 'type' => $type], | |
* ['key' => $key, 'value' => $value, 'type' => $type] | |
* ]; | |
*/ | |
function get_filter_parameters(array $filters = null):array | |
{ | |
if (!$filters) | |
return ['api.filter' => 0]; | |
$columns = ['api.filter' => 1]; | |
$index = 0; | |
foreach ($filters as $filter) | |
{ | |
$columns['api.filter_column_'.$index] = $filter['key']; | |
$columns['api.filter_term_' .$index] = $filter['value']; | |
$columns['api.filter_type_' .$index] = $filter['type']; | |
$index++; | |
} | |
return $columns; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once 'sort.php'; | |
require_once 'columns.php'; | |
require_once 'filter.php'; | |
/** | |
* @param array $columns 'List' of return property names to get. | |
* @param array $filters arrays each containing a 'column', 'term', and 'type' key. | |
*/ | |
function rpc( | |
string $hostName, | |
string $userName, | |
string $password, | |
string $moduleName, | |
string $functionName, | |
array $parameters = null, | |
array $columns = null, | |
array $filters = null, | |
array $sorters = null | |
):string | |
{ | |
$payload = $parameters | |
+ get_columns_parameters($columns) | |
+ get_filter_parameters($filters) | |
+ get_sort_parameters($sorters); | |
$options = [ | |
CURLOPT_DEFAULT_PROTOCOL => 'https', | |
CURLOPT_PROTOCOLS => CURLPROTO_HTTPS, | |
CURLOPT_SSL_VERIFYHOST => 2, | |
// CURLOPT_SSLVERSION => CURL_SSLVERSION_TLSv1_3, | |
CURLOPT_SSLVERSION => CURL_SSLVERSION_TLSv1_2, | |
CURLOPT_URL => "$hostname/execute/$module/$function", | |
CURLOPT_HTTPAUTH => CURLAUTH_BASIC, | |
CURLOPT_USERNAME => $username, | |
CURLOPT_PASSWORD => $password, | |
CURLOPT_PORT => $port, | |
CURLOPT_POST => true, | |
CURLOPT_POSTFIELDS => $payload, | |
CURLOPT_RETURNTRANSFER => true | |
]; | |
$curl = curl_init(); | |
curl_setopt_array($curl, $options); | |
$response = curl_exec($curl); | |
$info = curl_getinfo($curl); | |
curl_close($curl); | |
return $response; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once 'rpc.php'; | |
// Declare your username and password for authentication. | |
$username = 'example'; | |
$password = 'luggage12345'; | |
// Define the API call. | |
$cpanel_host = 'localhost'; | |
$request_uri = "https://$cpanel_host:2083/execute/SSL/install_ssl"; | |
$hostName = 'example.com'; | |
$userName = 'user'; | |
$password = 'luggage12345'; | |
$moduleName = 'DomainInfo'; | |
$functionName = 'list_domains'; | |
$parameters = null; | |
$columns = null; | |
$filters = null; | |
$sorters = null; | |
$response = rpc($hostName,$userName,$passwords,$moduleName,$functionName); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* takes an array of parameters and formats them into an array for UAPI RPC. | |
* $sorters = [ | |
* [ | |
* 'key' => $key, | |
* 'method' => $method, | |
* 'reverse' => $reverse | |
* ][,…] | |
* ]; | |
*/ | |
function get_sort_parameters(array $sorters = null):array | |
{ | |
if (!$sorters) | |
return ['api.sort' => 0]; | |
$params = ['api.sort' => 1]; | |
$index = 0; | |
foreach ($sorters as $sort) | |
{ | |
$params['api.sort_column_'.$index] = $sort['key']; | |
$params['api.sort_method_'.$index] = $sort['method']; | |
$params['api.sort_reverse_'.$index] = $sort['reverse']; | |
$index++; | |
} | |
return $params; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment