Jump to content

Recommended Posts

Hello,
I am trying to return with CURL my number of tickets with GetTicketCounts. However using the exact code of the documentation, I do not have a JSON which is returned. Also when I go to domain.com/includes/api.php I get this message: result = error; message = Authentication Failed.
Can you help me?
Thank you

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.domaine.hosting/includes/api.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt(
	$ch,
	CURLOPT_POSTFIELDS,
	http_build_query(
		array(
			'action' => 'GetTicketCounts',
			// See https://developers.whmcs.com/api/authentication
			'username' => 'apiCredentials',
			'password' => 'apiCredentials',
			'responsetype' => 'json'
		)
	)
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);

$jsonData = json_decode($response, true);
var_dump($jsonData);

 

Share this post


Link to post
Share on other sites

If I pass my parameters present in the table in the url I have the json that I need. In addition I will like to recover the awaitingReply value

Share this post


Link to post
Share on other sites

@CanadianDeer do you have an API user set up in WHMCS that is able to do those permissions? You will also need to whitelist your server.

Share this post


Link to post
Share on other sites
Posted (edited)

@CanadianDeer

I trust that this will work to show you how the API will work, along with my answer above...

 

<?php
error_reporting(0); //-set to E_ALL instead of 0 for debugging mode.
/***
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 = "https://example.com/whmcs"; //--your whmcs location

// 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 = "API Username";
$password = "API Password";

// Set post values
$postfields = array(
    'username' => $username,
    'password' => $password,
    'action' => 'GetClientsProducts',
    'serviceid'=> $srvid,
    '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); //$assoc=TRUE
//--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;
   //echo $item['suspensionreason'];
}
//--Variables for License Data
$susReason = $item['suspensionreason'];
$licStatus = $item['status'];
$nextDue = $item['nextduedate'];

As you can see, the API requires a username and password, along with permissions. This can be set up in Setup > Staff Management > Api Cridentials.  You may DM me if you have any questions, and I'll be glad to help. 

Also, the Decode response will help you to extract any data you want from the API command, as I did here for my license check 🙂

Edited by mfoland
Removed some personal info and fixed error reporting

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 and understand your posts will initially be pre-moderated