PHP logo

Integrate WorkOS using the PHP SDK

Easily add enterprise features like Single Sign-On (SSO/SAML), Directory Sync (SCIM), Audit Trail (SIEM), and Access Control (IAM) to your PHP app.

Installation

You can install the PHP SDK with Composer using the following command:

Terminal

composer require workos/workos-php

Usage

Below is an example integration that adds SSO using the PHP SDK. You can find other examples and more information in our PHP SDK Docs.

PHP

WorkOSWorkOS::setApiKey(getenv("WORKOS_API_SECRET_KEY"));
WorkOSWorkOS::setProjectId(getenv("PROJECT_ID"));

const DOMAIN = "example.com";
const REDIRECT_URI = "REDIRECT_URI";

switch (strtok($_SERVER["REQUEST_URI"], "?")) {
    case ("/auth/callback"):
        $profile = (new WorkOSSSO())->getProfile($_GET["code"]);
        header("Content-Type: application/json");
        echo json_encode($profile->toArray());
        return true;

    case ("/"):
    case ("/login"):
        $authUrl = (new WorkOSSSO())
            ->getAuthorizationUrl(
                DOMAIN,
                REDIRECT)URI,
                ["response" => "data"],
                null
            );
        header("Location: " . $authUrl);
        return true;

    default:
        header($_SERVER["SERVER_PROTOCOL"] . " 404");
        return true;
}

Start Integrating Today

Create an account to begin adding enterprise-ready features to your application today.