Jump to content

Return JSON


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);

 

Link to comment
Share on other sites

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

Guest
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