Skip to content

Latest commit

 

History

History
116 lines (76 loc) · 4.21 KB

php-azure.md

File metadata and controls

116 lines (76 loc) · 4.21 KB

title: Build API clients for PHP with Microsoft identity authentication description: Learn how use Kiota to build API clients in PHP to access APIs that require Microsoft identity authentication. author: Ndiritu ms.author: pgichuhi ms.topic: tutorial date: 03/20/2023

Build API clients for PHP with Microsoft identity authentication

In this tutorial, you generate an API client that uses Microsoft identity authentication to access Microsoft Graph.

Required tools

Create a project

Run the following commands in the directory where you want to create a new project.

composer init

Add dependencies

Before you can compile and run the generated API client, you need to make sure the generated source files are part of a project with the required dependencies. Your project must have a reference to the abstraction package. Additionally, you must either use the Kiota default implementations or provide your own custom implementations of the following packages.

For this tutorial, use the default implementations.

Run the following commands to get the required dependencies.

composer require microsoft/kiota-abstractions
composer require microsoft/kiota-http-guzzle
composer require microsoft/kiota-authentication-phpleague
composer require microsoft/kiota-serialization-json
composer require microsoft/kiota-serialization-text

Generate the API client

Kiota generates API clients from OpenAPI documents. Create a file named get-me.yml and add the following.

:::code language="yaml" source="~/code-snippets/get-started/azure-auth/get-me.yml":::

You can then use the Kiota command line tool to generate the API client classes.

kiota generate -l PHP -d get-me.yml -c GraphApiClient -n GetUser\Client -o ./client

Add the following to your composer.json to set your namespaces correctly:

"autoload": {
    "psr-4": {
        "GetUser\\Client\\": "client/"
    }
}

To ensure the newly generated classes can be imported, update the autoload paths using:

composer dumpautoload

Register an application

[!INCLUDE register-azure-app-auth-code-intro]

[!INCLUDE register-azure-app-auth-code-portal]

[!INCLUDE register-azure-app-auth-code-powershell]


Create the client application

Create a file in the root of the project named GetUser.php and add the following code. Replace the $clientId and $clientSecret with your credentials from the previous step.

[!INCLUDE get-azure-auth-code-manually]

Replace the $authorizationCode with your authorization code.

:::code language="php" source="~/code-snippets/get-started/azure-auth/php/GetUser.php" id="ProgramSnippet":::

Note

This example uses the AuthorizationCodeContext class. You can use any of the credential classes from the kiota-authentication-phpleague library.

Run the application

To start the application, run the following command in your project directory.

php GetUser.php

See also