
Originally Posted by
deieno
Hi,
take the script from the cpanel documentation:
function suspend ($host,$user,$accesshash,$usessl,$suspenduser) {
$result = whmreq("/scripts/remote_suspend?user=${suspenduser}",$host,$user,$accesshash,$usessl);
if ($cpanelaccterr != "") { return; }
return $result;
}
How can I convert this to get results from XML-API?
I use access hash and don't want to use password on my php script eighter
I apreceate if you give me a tip on how to do that
Here's an example from the XML API documentation:
/xml-api/suspendacct?user=bob&reason=no%20payment
So all you need to do is have your PHP script coded to access this URL. You can authenticate using headers and hashes with header:
Code:
Authentication: WHM $hash
Where $hash is your access hash.
While I'm not authorized to give advice regarding coding, you may want to take the code that will actually make the calls to the API and place it in a function similarly abstracted to your whmreq() function.
Here's some code I've been working on myself with regards to using the XML API with PHP (just a head's up that it's merely a work in progress since my test server is not liking me):
PHP Code:
// $hash = your hash
// $theServer = your server's hostname or IP
$apiPath = "/xml-api/listaccts";
// 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?
# 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 .= "Authorization: WHM " . $hash . "\r\n";
$header .= "Connection: close\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;