PHP code example of nti / community-client

1. Go to this page and download the library: Download nti/community-client library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

nti / community-client example snippets


$client = NTI\CommunityClient\Admin\CommunityClient::factory([
    'realm' => 'master',
    'username' => 'admin',
    'password' => '1234',
    'client_id' => 'admin-cli',
    'client_secret' => 'sfdasd',
    'baseUri' => 'http://127.0.0.1:8180',
]);

$client->getUsers();

//Result
// Array of users
/*
Array
(
    [0] => Array
        (
            'userid' => 1
            'usergroupid' => 0
            'membergroupids' =>
            'displaygroupid' => 0
            'username' => Anthony
            'email' => [email protected]
            'posts' => 5
            'deleted_posts' => 0
            'options' => 1024
            'autosubscribe' => 1
            'customoptions' => 7
            'topics' => 7
            'solved' => 1
            'ipaddress' =>
            'usertitle' => Pioneer
            'customtitle' => 1
            'pmunread' => 1
            'subscriptions' => 22
            'pmtotal' => 1
            'following' => 0
            'followers' => 0
            'avatar' => https://uploads-us-west-2.insided.com/alliant-en-sandbox/icon/200x200/4032f252-3c4a-46d7-83a0-5733ca2c3418.png
            'signature' =>
            'reputation' => 2
            'lastvisit' => 1614769533
            'lastactivity' => 1606228559
            'insided_sso_customeruid' =>
            'reviewcount' => 0
            'ratingcount' => 0
            'lastpostid' => 127
            'lastpost' => 1614771269
            'joindate' => 1403085355
            'likes' => 2
            'likes_given' => 3
            'blogposts' => 0
            'researches' => 0
            'rank_id' => 5
            'rank_name' => Inspiring
            'rank_display_name' => Inspiring
            'rank_avatar_icon' =>
            'rank_avatar_icon_thumb' =>
            'is_moderator' => 0
      )
)


$client->getUser([
    'id' => '39839a9b-de08-4d2c-b91a-a6ce2595b1f3'
]);

//Result

Array(
    'userid' => 3
    'usergroupid' => 0
    'membergroupids' =>
    'displaygroupid' => 0
    'username' => Eliza
    'email' => [email protected]
    'posts' => 3
    'deleted_posts' => 0
    'options' => 1024
    'autosubscribe' => 1
    'customoptions' => 7
    'topics' => 11
    'solved' => 1
    'ipaddress' => 54.182.244.102
    'usertitle' =>
    'customtitle' => 0
    'pmunread' => 0
    'subscriptions' => 22
    'pmtotal' => 1
    'following' => 0
    'followers' => 0
    'avatar' => https://uploads-us-west-2.insided.com/alliant-en-sandbox/icon/200x200/e481abd7-8942-4617-974a-0fa6c9d3a403.png
    'signature' =>
    'reputation' => 2
    'lastvisit' => 1662977253
    'lastactivity' => 1662982021
    'insided_sso_customeruid' =>
    'reviewcount' => 0
    'ratingcount' => 0
    'lastpostid' => 142
    'lastpost' => 1662977570
    'joindate' => 1563816419
    'likes' => 2
    'likes_given' => 1
    'blogposts' => 0
    'researches' => 0
    'rank_id' => 8
    'rank_name' => New Participant
    'rank_display_name' => New Participant
    'rank_avatar_icon' =>
    'rank_avatar_icon_thumb' =>
    'is_moderator' => 0
)

$client->createUser(['data' => [
            'email' => '[email protected]',
            'username' => 'test',
            'password' => true,
            'user_role' => [
                    'roles.registered',
                    'Option A',
                    'Option B',
                    'Other'
                ],
                'profile_field' => [
                    '12' => 'Smith',
                    '34' => 'John'
                ],
                'sso' => [
                    'facebook' => '333ABBDDD',
                    'oauth2' => '4545ag63274'],
                ]]);

//Result
Array(
    'user' => Array(
            'username' => 'test'
            'email' => '[email protected]',
            'joindate' => '1692394871'
            'userid' => 25
            'sso' => Array()
        )
)


$client->deleteUser([
    'id' => 1
]);
*/

  $client = NTI\CommunityClient\Admin\CommunityClient::factory([
      ...
      'grant_type' => 'password',
      'username' => 'admin',
      'password' => '1234',
  ]);
  

  $client = NTI\CommunityClient\Admin\CommunityClient::factory([
      ...
      'grant_type' => 'client_credentials',
      'client_id' => 'admin-cli',
      'client_secret' => '84ab3d98-a0c3-44c7-b532-306f222ce1ff',
  ]);
  
 
use GuzzleHttp\Middleware;
use Psr\Http\Message\RequestInterface;

$client = NTI\CommunityClient\Admin\CommunityClient::factory([
    ...
    'middlewares' => [
        // throws exceptions when request fails
        Middleware::httpErrors(),
        // other custom middlewares
        Middleware::mapRequest(function (RequestInterface $request) {
            return $request;
        }),
    ],
]);
 
class CustomTokenStorage implements TokenStorage 
{
    public function getToken() 
    {
        // TODO
    }
    
    public function saveToken(array $token)
    {
        // TODO
    }
}

$client = NTI\CommunityClient\Admin\CommunityClient::factory([
    ...
    'token_storage' => new CustomTokenStorage(),
]);

$client = CommunityClient::factory([
...
    'custom_operations' => [
        'getUsersByAttribute' => [
            'uri' => '/auth/realms/{realm}/userapi-rest/users/search-by-attr',
            'description' => 'Get users by attribute Returns a list of users, filtered according to query parameters',
            'httpMethod' => 'GET',
            'parameters' => [
                'realm' => [
                    'location' => 'uri',
                    'description' => 'The Realm name',
                    'type' => 'string',
                    '