Back

Step by Step Guide to Building a Microsoft Intune API Integration in PHP

Aug 8, 20246 minute read

Introduction

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!

Prerequisites

Before we jump in, make sure you've got these basics covered:

  • A PHP environment (you've got this, right?)
  • A Microsoft Azure account (if you don't have one, now's the time!)
  • An Intune subscription (because, well, we're working with Intune)

Setting up Azure AD App Registration

First things first, let's get your app registered with Azure AD:

  1. Head over to the Azure portal and create a new app registration.
  2. Configure the necessary permissions for Intune API access.
  3. Grab your client ID and client secret - you'll need these later!

Authentication

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; ?>

Making API Requests

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); ?>

Core Intune API Operations

Now that we're authenticated and making requests, let's explore some key Intune operations:

Device Management

// Get all devices $devices = $graph->createRequest("GET", "/deviceManagement/managedDevices") ->setReturnType(ManagedDevice::class) ->execute(); // Wipe a device $graph->createRequest("POST", "/deviceManagement/managedDevices/{id}/wipe") ->execute();

App Management

// 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();

Policy Configuration

// 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();

Error Handling and Logging

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()); }

Best Practices

Remember these key points:

  • Implement rate limiting to avoid hitting API thresholds
  • Cache responses when possible to improve performance
  • Always store your credentials securely (use environment variables!)

Testing the Integration

Before you call it a day, make sure to thoroughly test your integration:

  • Write unit tests for individual components
  • Perform integration tests to ensure everything works together smoothly

Conclusion

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! 🚀