
Originally Posted by
mikelegg
Thanks David
I was trying to use run it from another server, not the server itself.
If I log into WHM and paste the link into the address bar, I can get it to return the app list, but it prompts me to log in again.
What I really want to do is access the details of remote servers, not the local one- is there a way to do this?
Sure, here's a simplistic PHP script that demonstrates how you can connect to a remote server to execute API calls.
PHP Code:
// VARIABLES:
// $hash = your hash (not needed if using password authentication)
// $user = username for the reseller accompanying that hash
// $pass = password for that reseller (not needed if using hash authentication)
// $theServer = your server's hostname or IP
# What is the path to the API function you wish to use?
$apiPath = '/xml-api/gethostname';
// NOTE:
// THIS CODE WILL ONLY WORK IF YOU HAVE ENABLED
// OPENSSL IN PHP. YOU CAN DO THIS BY GOING TO WHM
// AND IN THE SOFTWARE SECTION, CLICK ON APACHE UPDATE
// THEN LOAD PREVIOUS CONFIG AND THEN CHECK THE BOX
// NEXT TO OPENSSL TO ENABLE SSL SUPPORT
//
// Of course, you could always go with http:// and 2086, but why?
# Make hash into one long string, in case it isn't already
$hash = str_replace("\n",'',$hash); // Note \r is not part of the newline indicator on *nix systems.
# Open a socket for HTTPS
$fp = fsockopen('ssl://' . $theServer, 2087, $errno, $errstr, 30);
# Uncomment to use unsecure HTTP instead
//$fp = fsockopen($theServer, 2086, $errno, $errstr, 30);
# Die on error initializing socket
if ($errno == 0 && $fp == FALSE) {
die('Socket Error: Could not initialize socket.');
} elseif ($fp == FALSE) {
die('Socket Error #' . $errno . ': ' . $errstr);
}
# Assemble the header to send
$header = '';
$header .= 'GET ' . $apiPath . " HTTP/1.0\r\n";
$header .= 'Host: ' . $theServer . "\r\n";
$header .= "Connection: Close\r\n";
$header .= 'Authorization: WHM ' . $user . ':' . $hash . "\r\n";
# Comment above line and uncomment below line to use password authentication in place of hash authentication
//$header .= 'Authorization: Basic ' . base64_encode($user . ':' . $pass) . "\r\n";
$header .= "\r\n";
# Send the Header
fputs($fp, $header);
# Get the raw output from the server
$rawResult = '';
while (!feof($fp)) {
$rawResult .= @fgets($fp, 128); // Suppress errors with @
}
# Close the socket
fclose($fp);
# Ignore headers
$rawResultParts = explode("\r\n\r\n",$rawResult);
$result = $rawResultParts[1];
# Output XML
echo $result;
With minimal knowledge of PHP, you can customize this code to make API calls to multiple servers. Just remember that access hashes are typically unique to each account on each server.
You will need reseller access or better on those remote servers to be able to use the APIs on those servers. Also note, you can't do anything via the API that you wouldn't be able to do via the WHM interface with the same credentials.