Jump to content

Get API data on tpl file


Recommended Posts


I am new to WHMCS and php and i would like to add a new custom php file that displays all the tlds and their pricing ... 

For that, i have created a new file : domainList.php under  /public_html/ directory


$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.mysite.com/includes/api.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
            'action' => 'GetTLDPricing',
            // See https://developers.whmcs.com/api/authentication
            'username' => 'GGT3m7EqO7bxnrb1HcT9i1z0tKOcmA03',
            'password' => 'qRFYmqc7JDNprtGxFFwKBJzHyaGxMSbv',
            'currencyid' => '1',
            'responsetype' => 'json',
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
$jsonData = json_decode($response, true);

add_hook('ClientAreaPage', 1, function($vars) {
    // Perform hook code here...
	return array("response" => $response);


i have added the part below so i can access the $response array in the domainList.tpl file

add_hook('ClientAreaPage', 1, function($vars) {
    // Perform hook code here...
    return array("response" => $response);

For now, i have some questions about my code

1- Is my code structured the right way ? should i place it under the API/ directory ?

2 - If so, how can i access mysite.com/Domain-Listing.php and call the service domainList.php that is under the API directory ?

3- How can i set a template to this file (TPL file) ? 

==> i saw some documentation here : https://developers.whmcs.com/advanced/creating-pages/ by using 

# Define the template filename to be used without the .tpl extension


4- is the add_hook method used the right way to access $response array directly from the TPL file ? 

==> In this case, i would do the following in the domainList.TPL file : 

{foreach $response['pricing'] as $tld => $price}
	{$tld} : {$price['register']['1']}<br>


For your information, this is the result contained in the response variable



Thanks in advance for the help !

Link to comment
Share on other sites

If you're building the code in the same directory as you WHMCS installation, use the local API instead. You just need to include the 'init.php' file. You should not place your files inside the api directory.

There's documentation on how to create custom pages for WHMCS here: https://developers.whmcs.com/advanced/creating-pages/
The add_hook function can only be used as a hook. You can not run it in a custom page. Any hooks you create will need to be placed in /includes/hooks/.

WHMCS also has data feeds that can provide you with product/domain pricing: https://docs.whmcs.com/Data_Feeds

Link to comment
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.

Reply to this topic...

×   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.

  • 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