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.

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.



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

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID

Close your account?

Your account will be closed and all data will be permanently deleted and cannot be recovered. Are you sure?