Hey there, fellow developer! Ready to dive into the world of Microsoft Intune API integration? You're in for a treat. This guide will walk you through the process of building a robust Intune API integration using PHP. We'll cover everything from setup to implementation, so buckle up and let's get coding!
Before we jump in, make sure you've got these basics covered:
First things first, let's get your app registered with Azure AD:
Now for the fun part - authentication! We'll be using OAuth 2.0 flow:
<?php $tokenEndpoint = 'https://login.microsoftonline.com/{tenant-id}/oauth2/v2.0/token'; $clientId = 'your-client-id'; $clientSecret = 'your-client-secret'; $scope = 'https://graph.microsoft.com/.default'; $tokenRequest = [ 'grant_type' => 'client_credentials', 'client_id' => $clientId, 'client_secret' => $clientSecret, 'scope' => $scope ]; $ch = curl_init($tokenEndpoint); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($tokenRequest)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); $token = json_decode($response)->access_token; ?>
With our token in hand, let's make some API calls:
<?php $apiEndpoint = 'https://graph.microsoft.com/v1.0/deviceManagement/managedDevices'; $ch = curl_init($apiEndpoint); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Authorization: Bearer ' . $token, 'Content-Type: application/json' ]); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); $devices = json_decode($response); ?>
Now that we're authenticated and making requests, let's explore some key Intune operations:
// Get all devices $devices = $graph->createRequest("GET", "/deviceManagement/managedDevices") ->setReturnType(ManagedDevice::class) ->execute(); // Wipe a device $graph->createRequest("POST", "/deviceManagement/managedDevices/{id}/wipe") ->execute();
// Get all apps $apps = $graph->createRequest("GET", "/deviceAppManagement/mobileApps") ->setReturnType(MobileApp::class) ->execute(); // Assign an app $graph->createRequest("POST", "/deviceAppManagement/mobileApps/{id}/assign") ->attachBody($assignmentBody) ->execute();
// Get all policies $policies = $graph->createRequest("GET", "/deviceManagement/deviceConfigurations") ->setReturnType(DeviceConfiguration::class) ->execute(); // Create a new policy $graph->createRequest("POST", "/deviceManagement/deviceConfigurations") ->attachBody($policyBody) ->execute();
Don't forget to implement proper error handling and logging:
try { // Your API call here } catch (GraphException $e) { error_log('Graph returned an error: ' . $e->getMessage()); } catch (Exception $e) { error_log('Error: ' . $e->getMessage()); }
Remember these key points:
Before you call it a day, make sure to thoroughly test your integration:
And there you have it! You've just built a Microsoft Intune API integration in PHP. Pretty cool, right? Remember, this is just the beginning - there's so much more you can do with the Intune API. Keep exploring, keep coding, and most importantly, have fun!
For more information, check out the Microsoft Graph API documentation and the PHP SDK for Microsoft Graph.
Now go forth and integrate! 🚀