
Originally Posted by
mstuebner
Even that I'm not one of them, here comes the code that finally works with hash:
REMARK: The below is NOT my style of programming, but I had to copy all in one function to make it "stand-alone".
PHP Code:
function sendRequestCurl() {
$accesshash = '2ef4c87d56b85a666dd10612b10065a8
9a3ef8ce4c95e488e4d111ed9710ce97
901ec100595dee38e71bb616e66ba649
05a9ad371df0f8c69aa42c8c2a1b9737
---- some parts cut out ------------
d0afb3baee2e9a7a22cf96b5c8cac90d
bc60a6089de5f49bbf31aac3f32ae9b6
3a8c55146a64a5762e0cc1ae4036ce7a';
$admin = "root";
$request = "/xml-api/listaccts?searchtype=user&search=";
$cleanaccesshash = preg_replace("'(\r|\n)'","",$accesshash);
$authstr = $admin . ":" . $cleanaccesshash;
$rurl = "https://ns.domain.net:2087" . $request;
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,0);
curl_setopt($ch, CURLOPT_URL, $rurl);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$curlheaders[0] = "Authorization: WHM $authstr";
curl_setopt($ch,CURLOPT_HTTPHEADER,$curlheaders);
$data=curl_exec ($ch);
curl_close ($ch);
if(!empty($data)) return(simplexml_load_string($data)); else return;
}
I hope it helps, I like to return something for the help I got.
Thanks, such a silly mistake on my part. Here's a revised version of my code:
PHP Code:
// $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;
Keep in mind this code is merely an example to assist you in learning how to use the API.