Select Page

Bobcares Blog

Tried and tested solutions for your servers, from our outsourced support diaries.

Extend WHMCS : Create Your own WHMCS API Functions

WHMCS is an awesome piece of software, and already a hot favorite of web hosting companies. The features it provides are more than sufficient for an average host to run his business. However, the environment of each host is unique in terms of market, vendors, and operation methods. This diversity often calls for customization of the WHMCS installation. For a web host which employs an experienced developer/company, this would be an simple task.

However, for a company completely new to WHMCS, this can be a conundrum. A common gripe among novice developers is the lack of detailed documentation on create API entensions to WHMCS. With this article I hope to reach out to those hosts who are stuck with a WHMCS API customization issue.

Hire Bobcares Server Administrators
Get super reliable servers and delighted customers

See how we do it!


I’ll give a basic explanation on the creation of WHMCS API using the example of a project I was working on. The requirement was to add private networking feature to a WHMCS VPS product. The service had no configurable options set up and no functions existed in the product module to trigger the request to database. So, the easiest way to do this was to make a custom API call which can be invoked in a customized module as well as in the client area.

All API functions are stored in the whmcsfolder/includes/api folder and so does our custom API. While creating APIs that update whmcs database, make sure that the code does not break anything else.

For security purposes, make sure nobody access your file directly :

if (!defined("WHMCS")) {
die("This file cannot be accessed directly");
}

When creating a WHMCS custom API call, its necessary to ensure you sanitize any input data you use.

Step 1 : Place the file

Create a file in whmcsfolder/includes/api/. Label it in the name of the API call you want make ie example.php, all lowercase, example will be the api call.

Step 2: Function to retrieve the parameters

When the API is called using local API, the CMD variable will be defined. This wont be defined when the call is made through POST. The parameters are defined in apivalues variable as shown below:

$array['action'] = $vars['cmd'];
$array['params'] = (object) $vars['apivalues1'];

The inputs can be validated by get_defined_vars()

Lets take a function get_env() to fetch the parameters and include the code mentioned above.

Step 3. The default API document

Before adding any functionality, create a default template with the function getParams() and the result.
Any result is returned in the variable array – $apiresults.
Check the sample file :

array(), 'params' => array());
if (isset($vars['cmd'])) {
//Local API mode
$param['action'] = $vars['cmd'];
$param['params'] = (object) $vars['apivalues1'];
$param['adminuser'] = $vars['adminuser'];
} else {
//Post CURL mode
$param['action'] = $vars['_POST']['action'];
unset($vars['_POST']['username']);
unset($vars['_POST']['password']);
unset($vars['_POST']['action']);
$param['params'] = (object) $vars['_POST'];
}
return (object) $param;
}

try {
// Get the arguments
$vars = get_defined_vars();
$postFields = getParam($vars);

$apiresults = array(“result” => “success”, “message” => “Success Message”);

} catch (Exception $e) {
$apiresults = array(“result” => “error”, “message” => $e->getMessage());
}

Step 4: Adding the required functionality in the template

Here we modify the default template mentioned above with our own additional queires, say for eg, inorder to update the custom fields with the VLAN allocated to the user, update the fields in whmcs, as mentioned below:

<?php

function getParams($vars) {
$param = array(‘action’ => array(), ‘params’ => array());
if (isset($vars[‘cmd’])) {
//Local API mode
$param[‘action’] = $vars[‘cmd’];
$param[‘params’] = (object) $vars[‘apivalues1’];
$param[‘adminuser’] = $vars[‘adminuser’];
} else {
//Post CURL mode
$param[‘action’] = $vars[‘_POST’][‘action’];
unset($vars[‘_POST’][‘username’]);
unset($vars[‘_POST’][‘password’]);
unset($vars[‘_POST’][‘action’]);
$param[‘params’] = (object) $vars[‘_POST’];
}
return (object) $param;
}

try {
// Get the arguments
$vars = get_defined_vars();
$postFields = getParam($vars);

// Block of code to update the whmcs details
$queryUpdate = “UPDATE tblcustomfieldsvalues SET value = ‘on’ WHERE relid = $serviceid and fieldid = ” . PAYG_PRIVATE_NETWORKING_CUSTOM_FIELD;
$queryResult = mysql_query($queryUpdate);
// Block ends here

$apiresults = array(“result” => “success”, “message” => “Success Message”);

} catch (Exception $e) {
$apiresults = array(“result” => “error”, “message” => $e->getMessage());
}

When the custom API is complete, this can be invoked using local API or using curl.

1.Using local API:

localAPI("example", array("serviceid" => $serviceid, “clientid” => $userid, “option” => “Active”), $adminUser);

2.Using CURL:

$postfields = array();
$postfields["action"] = "example";
$postfields["responsetype"] = "json";
$postfields["serviceid"] = $serviceid;
$postfields["clientid"] = $userid;
$postfields["option"] = "Active";
$results = $this->apiCurl($postfields);

where apiCurl() is the function that process curl request to whmcs api.

Thats about it. You now have your own custom API integration with WHMCS.

I hope this example based explanation has been clear enough. I’d be happy to answer any questions you have. Just post your questions related to WHMCS integration in our Google+ page.


About the author
is a senior software engineer at Bobcares. She is an expert in web application automation and customization. She is passionate about programming, and spends her free time polishing her skills in hacking new frameworks like Symfony, CakePHP, CodeIgniter, Smarty etc. She loves music, movies and travelling.







About Bobcares

Bobcares Bobcares is a server management company that helps businesses deliver uninterrupted and secure online services.
Our engineers manage close to 52,500 servers that include virtualized servers, cloud infrastructure, physical server clusters, and more.
MORE ABOUT BOBCARES