The following code samples are intended to provide developers with some core functions that are available to make use of in custom hooks, modules and addons for WHMCS to provide consistency with the rest of the system.
There are 3 date related functions you can use from WHMCS in custom modules to work with dates, for getting the current date, for converting a date/time field from the display into the chosen display format, and for convert an input date/time into the database format:
$todaysdate = getTodaysDate(); $date = fromMySQLDate($date,$time); $date = toMySQLDate($date);
In the fromMySQLDate function, $time can be a true/false value to determine if you want the time to be displayed with the date also.
These 2 functions can be used to format amounts for display in a clients currency. The first function retrieves the currency details for a given Client ID, and the second formats it for display. You must call the first function first unless you are only wanting to display a value in the default system currency.
$currency = getCurrency($userid); $amount = formatCurrency($amount);
Email Templates can be sent to clients as follows. Here the 3 parameters the function accepts are the email template name, and the ID to send it for (a Client ID for a general email template, a Service ID for a product related email, an Invoice ID for an invoice related email, etc...) and finally an optional extra variables parameter that allows you to define custom merge fields that should be available to the email template.
$messagename = 'Demo Email Template'; $relid = '1'; $extravars = array( 'custom_var1' => 'abc', 'another_var' => 'def' ); sendMessage($messagename,$relid,$extravars);
MySQL Database Queries
A DB connection is open and available to use from all WHMCS files. So you can simply use that with the regular mysql_query syntax, or you can make use of the SQL Helper Functions provided in WHMCS.
For any remote API connections, we recommend using CURL. And to simplify that, there is a built in call function you can use as follows.
$url = 'http://www.whmcs.com/'; $postfields = array( 'a' => 'b', 'c' => 'd', etc... ); #$options = array( 'CURLOPT_TIMEOUT' => '300', 'HEADER' => 'xxxxx' ); # Optional $response = curlCall($url,$postfields,$options);
The Local API makes available over 100 WHMCS functions to custom coding, for things like creating invoices, applying payments, creating tickets, managing products/domains, and more... For more details, please refer to the API:Internal_API section
Log Module Call
For provisioning and registrar modules, we recommend making use of the logModuleCall function to record all API requests and responses, to aid in debugging problems with the module. More details on this can be found in the Provisioning Module docs @ http://docs.whmcs.com/Provisioning_Module_Developer_Docs#Module_Logging
If you want to add an entry to the activity log, then you can do so using the LogActivity function as follows which will automatically record the message you specify along with the currently logged in admin or client, and IP address.
logActivity('Message goes here');