https://example.com:2087/xml-api/functionname, where example.com stands for your server's hostname or IP address, and functionname stands for the function you wish to access.
However, calling a function through a web browser is mainly useful for testing. The most powerful application of our API functions occur when they are incorporated into users' custom scripts. From within these scripts, there are 2 main ways of authenticating, outlined below.
#!/usr/bin/perl use strict; use LWP::UserAgent; use MIME::Base64; my $user = "root"; my $pass = "password"; my $auth = "Basic " . MIME::Base64::encode( $user . ":" . $pass ); print $auth; my $ua = LWP::UserAgent->new; my $request = HTTP::Request->new( GET => "http://127.0.0.1:2086/xml-api/listaccts" ); $request->header( Authorization => $auth ); my $response = $ua->request($request); print $response->content;
<?
$whmusername = "username";
$whmpassword = "password"
$query = "https://127.0.0.1:2087/";
$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.":".$whmpassword) . "\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);
print $result;
?>
/root/.accesshash.
#!/usr/bin/perl use strict; use LWP::UserAgent; my $hash = "3a8de68846a297de51f3921663972196 a298d16cc18ed4f9577753fc48e49917 3bd713ddfea78defc41c90cfe5aab2c7 7ff17d5c725efbc8fe058ba29b00d19f 6d6ea1848589e4312d3e13f2f44aa271 9bf5c4e15862be76ac39b201518961c6 581fa92b8ca67b53ef9b5ad331a3f845 586828f76b7e9cbba5ea7ae348d1207c dfdc0548c67bc46e13cd52d485b91b01 de43080c01d35e7cdfb9be3fb2925c85 616f599f109a1d5cd69a7171e8ed91d2 528bd0b8c162a395c8f50d7b01dd0918 b3573497549c5796926e50cd712d0c73 39a1403cc1a70ff8ed426c34320cdeaa e8190477c1fef7f300497176b34ebcca 87d014ac1c3c6d4fc33542ad8e5685c4 32f1303ff82ef50ebdcceaebd979656b 9798128753e538785bd931f915870359 9efb1ec80594160bba985fd8b3395fc9 58cda7cf250eea7a0d760fb9c3cd0232 6b8c720990e472bd369a75ec56ec9097 22b8800f93269cbcf73ac5b6a255f7e9 7dfce8543e4d7d0778c9e80ebbb97444 c39a16b41c3b7fd40140c407d13c7c71 75a3a6e37f4c8f85fe534c4cade7b220 6537bb25435a0d38bf54d20bea0ddce5 e07b17ccf9feb4285254ecb1e7f00e4f e821e5fdc0a81d531f3a8abc9848de63 998f28e37f5e50d93eab5a24cdb2bb28"; $hash =~ s/\n//g; my $auth = "WHM root:" . $hash; my $ua = LWP::UserAgent->new; my $request = HTTP::Request->new( GET => "http://127.0.0.1:2086/xml-api/listaccts" ); $request->header( Authorization => $auth ); my $response = $ua->request($request); print $response->content;
<?
$whmusername = "root";
$whmhash = "somelonghash"; # some hash value
$query = "https://127.0.0.1:2087/....";
$curl = curl_init(); # Create Curl Object
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,0); # Allow certs that do not match the domain
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,0); # Allow self-signed certs
curl_setopt($curl, CURLOPT_RETURNTRANSFER,1); # Return contents of transfer on curl_exec
$header[0] = "Authorization: WHM $whmusername:" . preg_replace("'(\r|\n)'","",$whmhash); # Remove newlines from the hash
curl_setopt($curl,CURLOPT_HTTPHEADER,$header); # Set curl header
curl_setopt($curl, CURLOPT_URL, $query); # Set your URL
$result = curl_exec($curl); # Execute Query, assign to $result
if ($result == false) {
error_log("curl_exec threw error \"" . curl_error($curl) . "\" for $query");
}
curl_close($curl);
print $result;
?>
Copyright © cPanel 2000-2009.