Jump to content
  • 0
Sign in to follow this  

Module function that returns json only


I have no idea how to pull this off in WHMCS so I'm hoping for some help.

I'm building a module that outputs some data to the client area (CPU usage and stuff like that). I really want this data to update every 5-10 seconds.
I know how to query the data - I just don't know how I can do this in WHMCS.

I thought about creating a custom function in the module that gets the relevant data and then returns it - but WHMCS seems to always want to render the client area 😅

What I basically need is a blank page where I can output json. I need the $params variable, so I can't do this outside of WHMCS.


Any ideas?


Share this post

Link to post
Share on other sites

5 answers to this question

Recommended Posts

  • 1

I see 3 possible solutions:

  • Output the json data at the _ClientArea function
  • Output the json data using a newly created php file
  • Output the json data using a hook

Output the json data at the ClientArea function

This is the easiest way. An example: https://pastebin.com/vSZ9Aie8

The disadvantage is the SSL checker from WHMCS. WHMCS checks for each _ClientArea function execution if a valid SSL certificate exists (and that can be very slow). However, I believe this check is only executed if the last check showed that no valid SSL certificate exists on the domain. And maybe the behavior has changed in the meantime - anyway, when the SSL checker came out there was this problem and personally I went over using the next option, which should also be somewhat less resource-heavy, because unwanted hooks are not executed. Not that it will make a significant performance difference, but well, if you're running this every few seconds and also possibly by multiple clients at the same time, it's certainly not wrong to pay attention to it as well, or at least be informed about it.

Output the json data using a newly created php file

You create a new file in your module directory, include the init.php of WHMCS (init.php is in the WHMCS root directory) so you can access all the WHMCS stuff (like the Eloquent ORM)  and then call this file via XHR: https://pastebin.com/rYKTYLEk

Important: Unlike the ClientArea function, you must ensure that the client has permission to access the service. So if you have a parameter for the service ID, you have to make sure that the user ID (e.g. $_S|E|S|S|I|O|N['uid'] (remove '|' - WHMCS WAF bypass, sorry)) belongs to this service. Otherwise, a customer could view the data of other customers by just changing the ID in the paramters.

Personally, I think this is the best approach, because you avoid the mentioned SSL checker problem of WHMCS and it is quite likely that this will also work in future WHMCS versions without any problems. 

Output the json data using a hook

It is also possible that you send output to the client via hook. Here is an example: https://pastebin.com/F9amWiJg

I would not go this route if you have access to the source code of the module.

2 hours ago, DennisHermannsen said:

I need the $params variable, so I can't do this outside of WHMCS.

It's easy to get the service model: https://pastebin.com/PBBxURX7

Side note

Ideally, you create a small HTTP controller to handle the AJAX requests, otherwise it gets confusing if you send many different XHR requests.

 I'm sorry for posting the code samples on pastebin and to obfuscate some parts. The WHMCS web application firewall blocks my post every time no matter how I modify the examples. I have tried for 15 minutes to get it working. A tech board where you can't post code correctly. Ridiculous, I can't even write SESSION correctly with a dollar sign in front of it. 🤷‍♂️🤦‍♂️

Edited by string

Share this post

Link to post
Share on other sites
  • 0

Thanks for the thorough explanation, @string!

I actually managed to find method 1 myself.
I'm not worried about SSL checks, though - the services that the module will manage does not have a domain set.

I didn't consider that WHMCS would call hooks for the ClientArea function on each call. I'll see if I can get it working using your other example.

One quick question: How would I access the module parameters if doing it that way? It's not just service params, it's stuff like the access hash that's defined in the provisioning module.

Share this post

Link to post
Share on other sites
  • 0

I think I found a rather simple way of doing what I'm trying to achieve.

https://pastebin.com/fYWhNxNr (I'm also having issues with their WAF, lol)

I just need to call /modules/servers/myModule/script.php?serviceId=<serviceId>.
The $server object has all the stuff I need to make API calls (access hash, hostname of the server etc.)

Any thoughts on that way to do it?

Share this post

Link to post
Share on other sites
  • 0
14 hours ago, DennisHermannsen said:

Any thoughts on that way to do it?

Yes, that would be the way it should be done. 

As an alternative you can rebuild the WHMCS module params using this code: https://pastebin.com/SgaBKj2G
ModuleBuildParams() should return exactly the same array as you see it in your module functions. But this function is not officially documentated, so I don't know if this function will be changed in a later version, or even exist at all.

Share this post

Link to post
Share on other sites
  • 0

To update data in the client area every 5-10 seconds, you can use JavaScript and the setInterval function to periodically send an AJAX request to your module and retrieve the updated data. Here is an example of how you can do this:In your module, create a function that returns the data you want to display in the client area as a JSON-encoded string. For example:


function mymodule_get_data($vars) {
    // Get data from your server or other source
    $data = array(
        'cpu_usage' => 50,
        'memory_usage' => 75,
        // ... other data here

    return json_encode($data);

In your module's client area template, add a JavaScript script that uses the setInterval function to send an AJAX request to your module's mymodule_get_data function every 5-10 seconds. For example:

setInterval(function() {
    // Send an AJAX request to your module's mymodule_get_data function
        url: '/modules/servers/mymodule/getdata.php',
        type: 'POST',
        dataType: 'json',
        success: function(response) {
            // Update the data in the client area using the response from the server
            $('#cpu_usage').text(response.cpu_usage + '%');
            $('#memory_usage').text(response.memory_usage + '%');
            // ... other data here
}, 5000); // update every 5 seconds

In your module's getdata.php file, add the following code to handle the AJAX request and return the data from your mymodule_get_data function:


// Load WHMCS environment
require_once __DIR__ . '/init.php';

// Get data from your module's mymodule_get_data function
$data = mymodule_get_data($vars);

// Return the data as a JSON-encoded string
echo $data;

This should allow you to update the data in the client area every 5-10 seconds using an AJAX request to your module. 

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated