FusionAuth PHP Client Library

PHP Client Library

The PHP client library allows you to integrate FusionAuth with your PHP application.

Source code:

Install the library

To use the client library on your project simply copy the PHP source files from the src directory to your project or the following Composer package.

Packagist

composer require fusionauth/fusionauth-client

Include composer autoloader

require __DIR__ . '/vendor/autoload.php';

Usage Suggestions

FusionAuth client libraries are a thin wrapper around the REST API. Client libraries are typically used in two different ways.

First, they can be used to access the FusionAuth APIs in a familiar format, leveraging language features like auto-completion. When used for this, they can be helpful to script FusionAuth configuration, automate common tasks, and create copies of existing applications, groups and more.

To use the client libraries effectively in this way, it is helpful to review the source code of the client library and the API documentation, which contains the JSON structure. The API documentation is very thorough about the JSON objects it expects as part of the payload as well as what parameters are required when.

Second, client libraries can exchange a token to let a user to log in via the Authorization Code Grant. This is a secondary use of these libraries. This process is best done by using a language specific OAuth library, which will work with FusionAuth. Here is a community curated list of such libraries.

Client libraries do not currently provide higher level functionality such as token management. Here is an open issue detailing some requested higher level functionality. Please feel free to file an issue or upvote this one if you desire it.

You can always directly call the REST API if the client library functionality doesn’t work for you. All the client libraries use the REST API.

In general, the request object will either be string parameters or a complex object depending on the type of API call being made. Any request object will be mapped by the library to a JSON object required by the corresponding API method. Examining the API documents for the operations you’re trying to call will therefore be useful, especially if you are using language without static typing.

The response object will typically contain:

  • a status corresponding to the HTTP status code returned by the API. It may also be -1 if no HTTP request was successfully made
  • a JSON success object if the call succeeded.
  • a JSON error object with an intelligible message if the status code is 4xx or 5xx.
  • an exception object if there was no HTTP request sent or there was no reasonable response from the server.

Create the Client

The following code assumes FusionAuth is running on http://localhost:9011 and uses an API key 5a826da2-1e3a-49df-85ba-cd88575e4e9d, you will need to supply your own API key, and if you are not running FusionAuth locally, your host parameter may be different.

$apiKey = "5a826da2-1e3a-49df-85ba-cd88575e4e9d";
$client = new FusionAuth\FusionAuthClient($apiKey, "http://localhost:9011");

Login a user

$applicationId = "68364852-7a38-4e15-8c48-394eceafa601";

$request = array();
$request["applicationId"] = $applicationId;
$request["loginId"] = "joe@fusionauth.io";
$request["password"] = "abc123";
$result = $client->login($request);
if (!$result->wasSuccessful()) {
 // Error
}

{/*  Hooray! Success */}

Example Apps

  • PHP Drupal quickstart - PHP Drupal quickstart tutorial showing how to integrate FusionAuth with a PHP Drupal application
  • PHP JWT - JWT creation and decoding examples with the PHP firebase-jwt library
  • PHP Laravel API quickstart - PHP Laravel API quickstart tutorial showing how to integrate FusionAuth with a PHP Laravel API
  • PHP Laravel quickstart - PHP Laravel quickstart tutorial showing how to integrate FusionAuth with a PHP Laravel application
  • PHP multi-tenant - A Pied Piper multi-tenant video chat application
  • PHP quickstart - PHP quickstart tutorial showing how to integrate FusionAuth with a PHP web application
  • Symfony 5 webapp - An Symfony 5 webapp using FusionAuth as the identity server
  • Updating a legacy PHP app using a connector - Updating a legacy PHP app to OAuth and migrating users
  • Webhooks - Handling a breached password detection webhook with PHP
  • Wordpress quickstart - Wordpress quickstart tutorial showing how to integrate FusionAuth with a Wordpress application

Other PHP Libraries

Here are other PHP libraries that may be useful. Some are community supported; please visit the library website to learn more.