PHP code example of fusionauth / fusionauth-client

1. Go to this page and download the library: Download fusionauth/fusionauth-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/ */

    

fusionauth / fusionauth-client example snippets




// $result is the response of one of the endpoint invocations from the examples below

if (!$result->wasSuccessful()) {
    echo "Error!" . PHP_EOL;
    echo "Got HTTP {$result->status}" . PHP_EOL;
    if (isset($result->errorResponse->fieldErrors)) {
        echo "There are some errors with the payload:" . PHP_EOL;
        var_dump($result->errorResponse->fieldErrors);
    }
    if (isset($result->errorResponse->generalErrors)) {
        echo "There are some general errors:" . PHP_EOL;
        var_dump($result->errorResponse->generalErrors);
    }
}

$client = new FusionAuth\FusionAuthClient(
    apiKey: "<paste the API Key you generated here>",
    baseURL: "http://localhost:9011", // or change this to whatever address FusionAuth is running on
);

$result = $client->createApplication(
    applicationId: null, // Leave this empty to automatically generate the UUID
    request: [
        'application' => [
            'name' => 'ChangeBank',
        ],
    ],
);

// Handle errors as shown in the beginning of the Examples section

// Otherwise parse the successful response
var_dump($result->successResponse->application);

$result = $client->createApplicationRole(
    applicationId: 'd564255e-f767-466b-860d-6dcb63afe4cc', // Existing Application Id
    roleId: null, // Leave this empty to automatically generate the UUID
    request: [
        'role' => [
            'name' => 'customer',
            'description' => 'Default role for regular customers',
            'isDefault' => true,
        ],
    ],
);

// Handle errors as shown in the beginning of the Examples section

// Otherwise parse the successful response
var_dump($result->successResponse->role);

$result = $client->retrieveApplication(
    applicationId: 'd564255e-f767-466b-860d-6dcb63afe4cc',
);

// Handle errors as shown in the beginning of the Examples section

// Otherwise parse the successful response
var_dump($result->successResponse->application);

$result = $client->deleteApplication(
    applicationId: 'd564255e-f767-466b-860d-6dcb63afe4cc',
);

// Handle errors as shown in the beginning of the Examples section

// Otherwise parse the successful response
// Note that $result->successResponse will be empty

$result = $client->deactivateUser(
    'fa0bc822-793e-45ee-a7f4-04bfb6a28199',
);

// Handle errors as shown in the beginning of the Examples section

// Otherwise parse the successful response

$result = $client->register(
    userId: 'fa0bc822-793e-45ee-a7f4-04bfb6a28199',
    request: [
        'registration' => [
            'applicationId' => 'd564255e-f767-466b-860d-6dcb63afe4cc',
            'roles' => [
                'customer',
            ],
            'data' => [
                'appBackgroundColor' => '#096324',
            ],
        ],    
    ],
);

// Handle errors as shown in the beginning of the Examples section

// Otherwise parse the successful response