PHPackages                             plai2010/php-msgraph - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. plai2010/php-msgraph

ActiveLibrary[API Development](/categories/api)

plai2010/php-msgraph
====================

Microsoft Graph utilities for PHP.

v1.2.3(9mo ago)0196MITPHP

Since Jul 24Pushed 9mo ago1 watchersCompare

[ Source](https://github.com/plai2010/php-msgraph)[ Packagist](https://packagist.org/packages/plai2010/php-msgraph)[ RSS](/packages/plai2010-php-msgraph/feed)WikiDiscussions main Synced 3w ago

READMEChangelogDependencies (3)Versions (6)Used By (0)

Microsoft Graph Utilities for PHP
=================================

[](#microsoft-graph-utilities-for-php)

This is a utility package for using Microsoft Graph API in PHP applications. In this version it only supports sending email messages to the `sendMail` endpoint.

The use case for this package was email notification in a Laravel (10.x) application. A Laravel service provider is included.

This package is designed to work with \[plai2010/php-oauth2\]\[plai2010-oauth2\] for OAuth2 token management.

Installation
------------

[](#installation)

The package can be installed from [Packagist](https://packagist.org/):

```
$ composer require plai2010/php-msgraph

```

One may also clone the source repository from `Github`:

```
$ git clone https://github.com/plai2010/php-msgraph.git
$ cd php-msgraph
$ composer install

```

Example: Sending Email
----------------------

[](#example-sending-email)

This package works best as a Laravel extension. It is *possible* to use it as just a utility library, but as one would see in this example, it can be a little awkard.

```
use PL2010\MsGraph\MsGraphClient;
use PL2010\OAuth2\Contracts\TokenRepository;

// Some PL2010\OAuth2\Contracts\TokenRepository.
// Here we have a single hard coded access token
// wrapped as a token repository.
$tkrepo = new class implements TokenRepository {
	public function getOAuth2Token(string $key, int $valid=0): array {
		return [
			// One single hard coded token regardless of $key.
			'access_token' => '....',
		];
	}
	public function putOAuth2Token(string $key, array $token): static {
		return $this;
	}
};

$msgraph = new MsGraphClient('sendmail', [
	// OAuth2 access token will be retrieved from the
	// token repository using this key.
	'token_key' => 'does-not-matter',
], [
	// Provide token repository.
	'token_repo' => $tkrepo,
]);

$msg = (new \Symfony\Component\Mime\Email())
	->from('no-reply@example.com')
	->to('john.doe@example.com')
	->subject('hello')
	->html('Greetings!')
	->attach(file_get_contents('wave.png'), 'wave.png', 'image/png')
;
$msgraph->sendEmail($msg);

```

Client Manager
--------------

[](#client-manager)

An application typically acts as a single MS Graph API client. [`MsGraphManager`](src/OMsGraphManager.php) allows multiple clients to be configured for more complex scenarios. For example, there may a client that acts for the application, and another as agent for individual users.

```
// Create manager.
$manager = new PL2010\MsGraph\MsGraphManager;

// Configure a application/service level client.
$manager->configure('service', [
	'token_key' => 'msgraph:app',
]);

// Configure an agent client. Here we assume that
// the token repository recognizes the '@user' tag
// in the token key and looks up user specific
// access token.
$manager->configure('agent', [
	'token_key' => 'msgraph:agent@user',
]);

```

Laravel Integration
-------------------

[](#laravel-integration)

This package includes a Laravel service provider that makes a singleton [`MsGraphManager`](src/MsGraphManager.php) available two ways:

```
* Abstract 'msgraph' in the application container, i.e. `app('msgraph')`.
* Facade alias 'MsGraph', i.e. `MsGraph::`.

```

The configuration file is `config/msgraph.php`. It returns an associative array of client configurations by names, like this:

```
