How to get all folders list in Microsoft Graph API with PHP Curl

We will talk about getting all folders list in Microsoft Graph API with Curl PHP.

Please refer the accesstoken from this link.

If you want to get a hidden folder list then pass includeHiddenFolders value will be true. For this API we require the below permission in your application. 
Permission type

 

Permission type Permissions
(work or school account) Mail.ReadBasic, Mail.Read, Mail.ReadWrite
Delegated (personal Microsoft account) Mail.ReadBasic, Mail.Read, Mail.ReadWrite
Application Mail.ReadBasic.All, Mail.Read, Mail.ReadWrite

 

Below is the CURL Request for get all folders list.

$url = "https://graph.microsoft.com/v1.0/me/mailFolders/?includeHiddenFolders=true&top=100";
$curlMailFolder = curl_init();
curl_setopt_array($curlMailFolder, array(
  CURLOPT_URL => $url,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_SSL_VERIFYHOST => false,
  CURLOPT_SSL_VERIFYPEER => true,
  CURLOPT_CUSTOMREQUEST => 'GET',
  CURLOPT_HTTPHEADER => array(
    'Content-Type: application/json',
    'Authorization: Bearer AccessToken'
  ),
));
$responseMailFolder = curl_exec($curlMailFolder);
curl_close($curlMailFolder);
$responseMailFolderData = json_decode($responseMailFolder, true);

In the response, you will get folder id, parent folder id, Folder Name, Unread count, Child folder Count, and also get boolean value in isHidden parameter true or false.

In some cases, you have a folder id and if you want to find only Specific folder details then you can also get using the CURL request you have to just change the URL in the above sample and you will get a response.

$url = "https://graph.microsoft.com/v1.0/me/mailFolders/{folderID}";

I hope it will help you.