Jump to content
mfoland

Licensing Addon Share -- helping you with the API

Recommended Posts

Hey all!

 

For those of you who use the Licensing Addon, and would want to get certain variables not originally shown in the licensing array such as suspension status, etc. I have an open source script for you that I have put together. I wouldn't mind possibly developing more into this and looking for donations.. but the source here is good. I was able to pull out the suspensionreason from WHMCS.

I named this file license_api.php and I use certain variables within my license.php file that does the license check. This allows you to grab more data from the actual license itself.

<?php
/***
Name: License API
Developer: WHMCS / Best PHP Scripts
Best Php Scripts created php_array to get license details from the server for any suspension reasons, etc.
Date created: 2/4/2019
**/

// API Connection Details
$whmcsUrl = "your whmcs url";

// For WHMCS 7.2 and later, we recommend using an API Authentication Credential pair.
// Learn more at http://docs.whmcs.com/API_Authentication_Credentials
// Prior to WHMCS 7.2, an admin username and md5 hash of the admin password may be used.
$username = "your api username";
$password = "your api password";

// Set post values
$postfields = array(
    'username' => $username,
    'password' => $password,
    'action' => 'GetClientsProducts',
    'serviceid'=> '17',
    'domain' => $license_key,
    'responsetype' => 'json',
);

// Call the API
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $whmcsUrl . 'includes/api.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postfields));
$response = curl_exec($ch);
if (curl_error($ch)) {
    die('Unable to connect: ' . curl_errno($ch) . ' - ' . curl_error($ch));
}
curl_close($ch);

// Decode response
$data = json_decode($response); 
//--Format our pretty array and loop it to get what we need from the call
$php_array = json_encode($data->{products}->{product});
$php_array = json_decode($php_array,true);
//print_r($php_array);
foreach($php_array as $item) { 
   $item = (array)$item;
}
//--Variables for Suspension
$susReason = $item['suspensionreason'];
$licStatus = $item['status'];

IF you want to do the script in development mode and see the full license array, you can simply uncomment the print_r($php_array); and that will allow you to see the full array. I then can call $susReason and even $licStatus in any script I so desire.. although for the original license status, I simply use $results[status] from the license call. I plan to expand on this script to get the reason the license is invalid. This is if you want to make a manage license page that shows if it's suspended, etc and they can put a new license key in. 

For the service id, you will need to do a variable such as $servid, which can be retrieved from your original license call. I forgot to put that fully into my script as I was doing this for a development test. ANY TIME you have a php variable in the api such as the license key etc, you cannot have the quote around it. The variable WILL NOT PASS. I've had to learn this one. 🙂

 

For my scripts, I store the key into a flat file that serves as a configuration.php type file, however I am considering changing my license key to be stored in the application database. I'm planning on ALSO storing the local key in the database so that the software doesn't have to call home every day.

 

Enjoy 🙂

 

Edited by mfoland

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×

Important Information

By using this site, you agree to our Terms of Use & Guidelines