CanadianDeer Posted June 6, 2019 Share Posted June 6, 2019 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); 0 Quote Link to comment Share on other sites More sharing options...
CanadianDeer Posted June 6, 2019 Author Share Posted June 6, 2019 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 0 Quote Link to comment Share on other sites More sharing options...
mfoland Posted June 7, 2019 Share Posted June 7, 2019 @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. 0 Quote Link to comment Share on other sites More sharing options...
mfoland Posted June 7, 2019 Share Posted June 7, 2019 (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 June 7, 2019 by mfoland Removed some personal info and fixed error reporting 0 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.