PHP Client Library
On this page
The PHP client library integrates FusionAuth with your PHP application. The source code is in the PHP client GitHub repository.
Install#
To install the library, choose one of the following options:
- Copy the PHP source files from the
srcdirectory into your project. - Install the Packagist package with Composer.
To install the Composer package, run:
$ composer require fusionauth/fusionauth-client
Then include the Composer autoloader:
require __DIR__ . '/vendor/autoload.php';
Create the Client#
The following examples assume FusionAuth runs at http://localhost:9011. Each example contains an API key. Replace the API key with your own. When FusionAuth doesn't run locally, replace the host URL too.
$apiKey = "5a826da2-1e3a-49df-85ba-cd88575e4e9d";
$client = new FusionAuth\FusionAuthClient($apiKey, "http://localhost:9011");
Log In a User#
The following code logs in a User with a login Id and password:
$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 */}
Usage Suggestions#
Choose a Use Case#
Client libraries wrap the REST API. You can use a client library for two tasks.
The main task is calling the FusionAuth APIs from code. You get auto-completion and typed objects, which helps when you script configuration, automate common tasks, or copy existing Applications and Groups. The API documentation lists the JSON structure of every request and which fields each call requires. When you need the details, read the API documentation and the library source code.
The secondary task is exchanging an authorization code for tokens in the Authorization Code grant. For this task, use an OAuth library for your language instead. For a list of libraries, see the community curated list.
Client libraries don't provide higher level functionality such as token management. To request that functionality, comment on or upvote the open issue.
When a client library doesn't fit your needs, call the REST API directly.
Requests and Responses#
Each library maps a request object to the JSON that the matching API method requires. Depending on the API call, the request object is a set of string parameters or a complex object. When you use a language without static typing, read the API documentation for the operation to learn the expected fields.
Each library returns a response object with these fields:
- A status that matches the HTTP status code from the API. The status is
-1when no HTTP request succeeded. - A JSON success object when the call succeeds.
- A JSON error object with a message when the status is
4xxor5xx. - An exception object when the library sent no request or the server returned no usable response.
Use Separate Clients for API Key Scopes#
When a script calls endpoints with different API key scopes, create one client instance per scope. For example, Entities accept a tenant-scoped API key, but Entity Types and Lambdas require a global API key.
Follow these rules when your library lets you set a Tenant Id on the client instance:
- Set the Tenant Id on the tenant-scoped client only.
- Don't change the Tenant Id on a shared client between calls.
- Don't send a tenant header to a global-only endpoint. The endpoint fails with a
401.
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#
The community supports some of the following PHP libraries. For more information, visit the website of each library.
- WordPress OpenID Connect Plugin
- FusionAuth Provider for the PHP League's OAuth 2.0 Client
- FusionAuth Provider for Laravel Socialite
Update Policy#
FusionAuth releases new versions of the SDKs and client libraries to track the FusionAuth API. SDKs and client libraries may also receive bug fixes, security patches, tests, code samples, and documentation changes.
These releases may update dependencies, language engines, and operating systems, because FusionAuth follows the deprecation and sunsetting policies of the underlying technologies. When the maintainer deprecates a language, framework, or operating system, FusionAuth deprecates the SDKs and client libraries that depend on the technology. FusionAuth then updates the SDKs and client libraries to use a newer version.
For more information, see Update a Client Library.