Jump to content

Retrieve all clients that an user id can manage, via API


JavierCN
Go to solution Solved by JavierCN,

Recommended Posts

Hello everyone,

I need to retrieve a list of all clients that a user can manage through the WHMCS API. I've searched the documentation and community discussions, but unfortunately, I haven't been able to find a solution.

The goal is to fetch the clients ids associated with a user id.

Thank you in advance for your assistance!

Javier

Edited by JavierCN
Link to comment
Share on other sites

  • JavierCN changed the title to Retrieve all clients that an user id can manage, via API
  • 2 weeks later...
  • Solution

I answer myself, with the aid of the WHMCS support team and  @pRieStaKos:

The GetUsers API command has a field to filter by email. It can be used to retrieve the user you want.

This command will display all the clients managed by the user.

Currently, there is no option to filter users by ID to get their assigned clients.

Edited by JavierCN
Full explanation
Link to comment
Share on other sites

Please try this code. 

<!DOCTYPE html>
<html>
<head>
    <title>Fetch Client Details by User ID</title>
</head>
<body>
    <h1>Fetch Client Details by User ID</h1>
    <form method="post">
        Enter User ID: <input type="text" name="userId">
        <input type="submit" value="Fetch Client Details">
    </form>
    <?php
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        $whmcsUrl = "https://whmcs.domain.com/";
      $api_identifier = "your_api_credential_identifier";
        $api_secret = "your_api_credential_secret";

        $userID = $_POST["userId"];


        $postfields = array(
            'identifier' => $api_identifier,
            'secret' => $api_secret,
            'action' => 'GetClientsDetails',
            'clientid' => $userID, // Set the User ID from the form
            'responsetype' => 'json',
        );

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

        $jsonData = json_decode($response, true);

        if ($jsonData['result'] == 'success') {
            $clientDetails = $jsonData['client'];
            $firstname = $clientDetails['firstname'];
            $lastname = $clientDetails['lastname'];
            $email = $clientDetails['email'];

            echo '<h2>Client Details for User ID ' . $userID . '</h2>';
            echo 'First Name: ' . $firstname . '<br>';
            echo 'Last Name: ' . $lastname . '<br>';
            echo 'Email: ' . $email . '<br>';
        } else {
            echo 'API call failed: ' . $jsonData['message'];
        }
    }
    ?>
</body>
</html>

 

Link to comment
Share on other sites

Hello @techwthquestion:

The ClientID is not the same than the UserID, so the query GetClientsDetails cannot be used to filter by the UserID.

Results:
{
   "result": "success",
   "userid": 1147,
   "client_id": 1147,
   "id": 1147,
   "owner_user_id": 2,

...

 

As you see, the owner_user_id is not the same as "client_id" (I guess that the field "userid" is maintained equal to client_id for compatibility purposes).

In that query, you can filter by ClientID, and you can see the user IDs that have permissions over the client, but I need a command that do the opposite (filter by UserID to get the Clients managed by that user).

Thank you anyway.

Link to comment
Share on other sites

  • 1 year later...
23 hours ago, Burti said:

U get userid but u dont know user_clients.*.id 

You get that with the GetUsers end point: https://developers.whmcs.com/api-reference/getusers/

{
    "result": "success",
    "totalresults": 1,
    "startnumber": 0,
    "numreturned": 1,
    "users": [
        {
            "id": 1,
            "firstname": "John",
            "lastname": "Smith",
            "email": "john.smith@example.net",
            "datecreated": "2020-12-15 15:29:49",
            "validationdata": "",
            "clients": [
                {
                    "id": 1,
                    "isOwner": true
                }
            ]
        }
    ]
}

 

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