> ## Documentation Index
> Fetch the complete documentation index at: https://docs-dev-actions-triggers-prototype.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# PHP：Auth0-PHPでManagement APIを使用する

> Auth0のManagement APIエンドポイントをPHPアプリケーションに統合する

Auth0 PHP SDKには`Auth0\SDK\API\Management`クラスが用意されています。このクラスには、[Management API](https://auth0.com/docs/api/management/v2)にアクセスし、Auth0テナントで操作を実行するのに使用できるメソッドが格納されています。このインターフェイスを使用すると、以下のことが簡単にできます。

* ユーザーの検索と作成
* アプリケーションの作成と更新
* ログエントリーの取得
* ルールを管理する

このほかにも多数のことができます。詳細については、「[APIリファレンス](https://auth0.com/docs/api/management/v2)」を参照してください。認証

<Tooltip data-tooltip-id="react-containers-DefinitionTooltip-0" href="/docs/ja-jp/glossary?term=management-api" tip="Management API: 顧客が管理タスクを実行できるようにするための製品。" cta="用語集の表示">Management API</Tooltip>を使用するには、以下のいずれかの方法で認証を行う必要があります。

* 一時的にアクセスするかテストを実施する場合には、[APIトークンを手動で生成し](/docs/ja-jp/secure/tokens/access-tokens/management-api-access-tokens)、`.env`ファイルに保存します。
* 拡張アクセスでは、アクセスが必要な場合にはクライアント資格情報付与を作成して実行する必要があります。このプロセスの詳細については、[Authentication APIページ](/docs/ja-jp/libraries/auth0-php/using-the-authentication-api-with-auth0-php)をご覧ください。

いずれの方式でも、生成されたトークンにはアプリで実行する操作に必要なスコープが含まれている必要があります。アクセスしようとする特定のエンドポイントに必要なスコープについては、[APIドキュメント](/docs/ja-jp/api/management/v2)を参照してください。

必要なスコープを付与するには、以下のようにします。

1. [［API］](https://manage.auth0.com/#/apis) >［Auth0 Management API］> **［Machine to Machine Applications（マシンツーマシンアプリケーション）］** タブの順に移動します。
2. アプリケーションを見つけて認可します。
3. 矢印をクリックして列を拡張し、必要なスコープを選択します。

上記のいずれかの方法を認証し、そのトークンを使って操作を実行できるようになります。

```php lines theme={null}
// 👆 We're continuing from the "getting started" guide linked in "Prerequisites" above. Append this to the index.php file you created there.

if (isset($env['AUTH0_MANAGEMENT_API_TOKEN'])) {
    $auth0->configuration()->setManagementToken($env['AUTH0_MANAGEMENT_API_TOKEN']);
}

// Create a configured instance of the `Auth0\SDK\API\Management` class, based on the configuration we setup the SDK ($auth0) using.
// If no AUTH0_MANAGEMENT_API_TOKEN is configured, this will automatically perform a client credentials exchange to generate one for you, so long as a client secret is configured.
$management = $auth0->management();
```

`Management`クラスはエンドポイントへのアクセスをインスタンスのファクトリメソッドとして保存します。たとえば、`$management->users()`は/users Management APIエンドポイントの操作に使用できる`Auth0\SDK\API\Management\Users`のインスタンスを返します。

### 例 - Search Users by Email（メールでユーザーを検索する）

このエンドポイントは[こちらに](/docs/ja-jp/api/management/v2#!/Users/get_users)記載されています。

```php lines theme={null}
// 👆 We're continuing from the code above. Append this to your source code file.

$response = $management->users()->getAll(['q' => 'josh']);

// Does the status code of the response indicate failure?
if ($response->getStatusCode() !== 200) {
    die("API request failed.");
}

// Decode the JSON response into a PHP array:
$response = json_decode(response->getBody()->__toString(), true, 512, JSON_THROW_ON_ERROR);

if (! empty($response)) {
    echo '<h2>User Results</h2>';

    foreach ($response as $result) {
        printf(
            '<p><strong>%s</strong> &lt;%s&gt; - %s</p>',
            !empty($result['nickname']) ? $result['nickname'] : 'No nickname',
            !empty($result['email']) ? $result['email'] : 'No email',
            $result['user_id']
        );
    }
}
```

### 例 - Get All Clients（すべてのクライアントを取得する）

このエンドポイントは[こちらに](/docs/ja-jp/api/management/v2#!/Clients/get_clients)記載されています。

```php lines theme={null}
// 👆 We're continuing from the code above. Append this to your source code file.

$response = $management->clients()->getAll(['q' => 'josh']);

// Does the status code of the response indicate failure?
if ($response->getStatusCode() !== 200) {
    die("API request failed.");
}

// Decode the JSON response into a PHP array:
$response = json_decode(response->getBody()->__toString(), true, 512, JSON_THROW_ON_ERROR);

if (! empty($response)) {
    echo '<h2>Get All Clients</h2>';

    foreach ($response as $result) {
        printf(
            '<p><strong>%s</strong> - %s</p>',
            $result['name'],
            $result['client_id']
        );
    }
}
```

## もっと詳しく

* [PHP：Auth0-PHPを使用して始める](/docs/ja-jp/libraries/auth0-php)
* [PHP：Auth0-PHPを使用したログイン、ログアウト、ユーザープロファイルの返送](/docs/ja-jp/libraries/auth0-php/auth0-php-basic-use)
* [PHP：Auth0-PHPでAuthentication APIを使用する](/docs/ja-jp/libraries/auth0-php/using-the-authentication-api-with-auth0-php)
* [PHP：Auth0-PHPを使用したJWT（JSON Web Token）の検証](/docs/ja-jp/libraries/auth0-php/validating-jwts-with-auth0-php)
* [PHPAuth0-PHP統合のトラブルシューティング](/docs/ja-jp/libraries/auth0-php/troubleshoot-auth0-php-library)
