API Usage

Communicating with the WHMCS API is very simple. The following PHP code will create a Project in WHMCSPM2, and is how we recommend interfacing with it. Something to remember, all WHMCSPM2 and WHMCS admin roles are ignored when using the API.

External API Usage:

$url = "http://www.yourdomain.com/whmcs/includes/api.php";
$username = "Admin"; # Admin username goes here
$password = "demo"; # Admin password goes here

$postfields["username"] = $username;
$postfields["password"] = md5($password);
$postfields["action"] = "whmcspm2addproject";
$postfields["name"] = "Sample API created project";
$postfields["description"] = "This is a description";
$postfields["category"] = "1";
$postfields["priority"] = "1";
$postfields["client"] = "2212";
$postfields["status"] = "2";
$postfields["readonly"] = "0";
$postfields["private"] = "0";
$postfields["admin_email_ok"] = "1";
$postfields["client_email_ok"] = "1";
$postfields["assigned"] = "1,2,5";
$postfields["startdate"] = "2011-11-03";
$postfields["enddate"] = "2012-01-22";
$postfields["creatortype"] = "1";
$postfields["creator"] = "4";
$postfields["budget"] = "250.00";
$postfields["sendemail"] = "0";
$customfields = array("1" => "Some Value","2" => "Some Other Value");
$postfields["customfields"] = base64_encode(serialize($customfields));

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 100);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
$data = curl_exec($ch);
curl_close($ch);

$data = explode(";",$data);
foreach ($data AS $temp) {
    $temp = explode("=",$temp);
    $results[$temp[0]] = $temp[1];
}

if ($results["result"]=="success") {
    # Result was OK!
} else {
    # An error occured
    echo "The following error occured: ".$results["message"];
}

Internal API Usage:

# Set Vars
$command = 'whmcspm2addproject';

$values["action"] = "whmcspm2addproject";
$values["name"] = "Sample API created project";
$values["description"] = "This is a description";
$values["category"] = "1";
$values["priority"] = "1";
$values["client"] = "2212";
$values["status"] = "2";
$values["readonly"] = "0";
$values["private"] = "0";
$values["admin_email_ok"] = "1";
$values["client_email_ok"] = "1";
$values["assigned"] = "1,2,5";
$values["startdate"] = "2011-11-03";
$values["enddate"] = "2012-01-22";
$values["creatortype"] = "1";
$values["creator"] = "4";
$values["budget"] = "250.00";
$values["sendemail"] = "0";
$customfields = array("1" => "Some Value","2" => "Some Other Value");
$values["customfields"] = base64_encode(serialize($customfields));
$adminuser = 'DemoAdmin';

# Call API
$results = localAPI($command,$values,$adminuser);
if ($results['result']!="success") {
    echo "An Error Occurred: ".$results['result'];
}
Was this answer helpful? 43 Users Found This Useful (79 Votes)