Skip to main content

Version 1.x

Using the API in v1

To make use of the API, start by creating a new GraphClient:

use Qore\Graph\GraphClient;

$client = new GraphClient();

Now you can authenticate:

$client->authenticate($tenantId, $clientId, $clientSecret);
// Or:
$client->authenticate(
tenantId: setting('qore/microsoft-graph', 'graph_tenant_id'),
clientId: setting('qore/microsoft-graph', 'graph_client_id'),
clientSecret: setting('qore/microsoft-graph', 'graph_client_secret'),
);

Below you can find a few examples that might help you get started. The full Microsoft Graph documentation can be found here.

Examples

info

It might be easier to find examples using ChatGPT. For example:

Provide me PHP code to create an event in Microsoft Graph with the following conditions:
- We are authorized as a tenant and not as a user
- We are using a Guzzle client

Send e-mail

Once authenticated, you can start doing requests. For example, let's send an e-mail:

caution

Please be aware of the json => [ ... ] array key that is prefixed before the body. If you don't do this, your request will fail.

$client = new GraphClient();
$client->authenticate($tenantId, $clientId, $clientSecret);

$sender = 'koen@qlic.nl';
$receiver = 'koen@qlic.nl';
$content = 'Hello <strong>World</strong>';

$client->post("v1.0/users/$sender/sendMail", [
'json' => [
'message' => [
'subject' => 'Your test e-mail',
'body' => [
'contentType' => 'HTML',
'content' => $content
],
'toRecipients' => [
[
'emailAddress' => [
'address' => $receiver
]
]
]
],
'saveToSentItems' => 'false'
]
]);

Above example requires the following permission: Graph -> Application permissions -> Mail -> Mail.Send

Get users

$client = new GraphClient();
$client->authenticate(
tenantId: setting('qore/microsoft-graph', 'graph_tenant_id'),
clientId: setting('qore/microsoft-graph', 'graph_client_id'),
clientSecret: setting('qore/microsoft-graph', 'graph_client_secret'),
);


$users = json_decode($client->get('v1.0/users')->getBody()->getContents(), true);

Above example requires the following permission: Graph -> Application permissions -> User -> Users.Read.All

Find user by e-mail

$client = new GraphClient();
$client->authenticate(
tenantId: setting('qore/microsoft-graph', 'graph_tenant_id'),
clientId: setting('qore/microsoft-graph', 'graph_client_id'),
clientSecret: setting('qore/microsoft-graph', 'graph_client_secret'),
);

$user = json_decode(
$client->get("v1.0/users?\$filter=mail eq 'koen@qlic.nl'")->getBody()->getContents(),
true
)['value'][0];

Above example requires the following permission: Graph -> Application permissions -> User -> Users.Read.All

Create outlook event

  • $start: 2024-12-21T12:00:00
  • $end: 2024-12-21T14:00:00
$client = new GraphClient();
$client->authenticate(
tenantId: setting('qore/microsoft-graph', 'graph_tenant_id'),
clientId: setting('qore/microsoft-graph', 'graph_client_id'),
clientSecret: setting('qore/microsoft-graph', 'graph_client_secret'),
);

$event = [
'subject' => 'Test event!',
'body' => [
'content' => 'hello',
'contentType' => 'HTML'
],
'start' => [
'dateTime' => $start,
'timeZone' => "Europe/Amsterdam"
],
'end' => [
'dateTime' => $end,
'timeZone' => "Europe/Amsterdam"
]
];

$response = $client->post('v1.0/users/koen@qlic.nl/events', ['json' => $event]);
$object = json_decode($response->getBody()->getContents(), true);

Above example requires the following permission: Graph -> Application permissions -> Calendars -> Calendars.ReadWrite

Find scheduled events for user

$client = new GraphClient();
$client->authenticate(
tenantId: setting('qore/microsoft-graph', 'graph_tenant_id'),
clientId: setting('qore/microsoft-graph', 'graph_client_id'),
clientSecret: setting('qore/microsoft-graph', 'graph_client_secret'),
);

$response = $client->post(
"v1.0/users/koen@qlic.nl/calendar/getSchedule",
[
'json' => [
'schedules' => ['koen@qlic.nl'],
'startTime' => [
'dateTime' => '2023-06-27T09:00:00',
'timezone' => 'Europe/Amsterdam'
],
'endTime' => [
'dateTime' => '2023-06-29T18:00:00',
'timezone' => 'Europe/Amsterdam'
],
'availabilityViewInterval' => 60
]
]
);

$items = json_decode($response->getBody()->getContents(), true);

Above example requires the following permission: Graph -> Application permissions -> Calendars -> Calendars.Read