0

I followed this tutorial to set up a laravel enviroment to work with the Microsoft Graph API. I want to do CRUD operations on Office 365 users, but in the documentation, there is no PHP sample code provided.

This is what the request body should look like:

POST https://graph.microsoft.com/v1.0/users
 Content-type: application/json

 {
   "accountEnabled": true,
   "displayName": "displayName-value",
   "mailNickname": "mailNickname-value",
   "userPrincipalName": "upn-value@tenant-value.onmicrosoft.com",
   "passwordProfile" : {
   "forceChangePasswordNextSignIn": true,
   "password": "password-value"
  }
 }

However, How do I translate that to a PHP call?

IlGala
  • 3,043
  • 3
  • 31
  • 46
Y_Lakdime
  • 743
  • 2
  • 9
  • 23

1 Answers1

0

You need to post that as a PHP Curl request. Here is the basic idea to get you started.

$jsonStr = json_encode(Array(
    "param1"=>"val1",
    "param2"=>"val2",
    "param3"=>Array(
                "param1"=>"val1",
                "param2"=>"val2"
    )
));
$headers = Array("Content-Type: application/json","Content-Length:".strlen($jsonStr));

$ch = curl_init("https://graph.microsoft.com/v1.0/users");
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
  • Thanks, how should I do it with that nested object 'passwordProfile'? – Y_Lakdime Oct 03 '19 at 10:59
  • The PHP function json_encode can encode nested arrays, I have update the example to show nested – Adam Whateverson Oct 03 '19 at 12:43
  • thanks for the update. i know this is a bit out of scope, but where in the laravel framework do I put this post request to actually be executed? I am a total Angular user and new to Php so please bear with me :) – Y_Lakdime Oct 03 '19 at 14:32
  • I don't use Laravel sorry. For me it looks completely foreign and if you have already managed to get access tokens you should have a good idea roughly on what to do next. In all honesty, I would go to a Laravel forum and ask that question as it is very specific to them. This might help? https://laravel.com/docs/5.2/requests – Adam Whateverson Oct 04 '19 at 09:55
  • Hi @Adam Whateverson, sorry to bother you again but how do I implement the auth token into the $headers variable, so I can create users to my Office365 tenant application? – Y_Lakdime Oct 07 '19 at 14:18
  • It's a big question to ask but here's my process for my project. 1). Request an Authorisation Code from https://login.microsoftonline.com/common/oauth2/authorize and pass it the client_id, response_type=code and response_uri 2) Once I have that code,I exchange it for an access token by sending it to https://login.microsoftonline.com/common/oauth2/token and using grant_type "authorization_code". 3). With my access token I can make graph requests as long as the access token is included in a header via Authorization: Bearer [access_token] That's the basic flow. – Adam Whateverson Oct 08 '19 at 10:31