Hello
I give some progress, for example how to download a file from a sharepoint using the Microsoft Graph REST API (no library needed).
I recommend watching this video to know how to configure and pre-register the application in AzureAD and give API permissions correctly before running the code:
//site_id consists of site_id and web_id, separated by ,
//site id->https://xxxxxxxxxxxxxxx.sharepoint.com/sites/<<site_name>>/_api/site/id
//web_id->https://xxxxxxxxxxxxxxx.sharepoint.com/sites/<<site_name>>/_api/web/id
$access_token = getAccessToken($tenant_id, $client_id, $client_secret);
$file_url = “https://graph.microsoft.com/v1.0/sites/".$domain.",".$site_id."/drive/root:/”.$file_path;
$ch = curl_init($file_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer ’ . $access_token
));
$file_content = curl_exec($ch);
$file_data = json_decode($file_content, true);
if ($file_data && isset($file_data[’@microsoft.graph.downloadUrl’])) {
$download_url = $file_data[’@microsoft.graph.downloadUrl’];
} else {
echo “Error.”;
}
$file_info = curl_getinfo($ch);
curl_close($ch);
$file_content = file_get_contents($download_url);
if ($file_content !== false) {
file_put_contents($local_file_path, $file_content);
} else {
echo “Error.”;
}
function getAccessToken($tenant_id, $client_id, $client_secret) {
$token_url = “Sign in to your account”; //https://login.microsoftonline.com/$tenant_id/oauth2/v2.0/token
$token_data = array(
‘grant_type’ => ‘client_credentials’,
‘client_id’ => $client_id,
‘client_secret’ => $client_secret,
‘scope’ => ‘https://graph.microsoft.com/.default’
);
$ch = curl_init($token_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($token_data));
$response = curl_exec($ch);
curl_close($ch);
$token_info = json_decode($response, true);
return $token_info['access_token'];
}