Create a php graph upload to onedrive

Andreas Rottmann 0 Reputation points
2024-04-28T14:53:06.51+00:00

Hello

i did the php graph tutorial to create a upload script for onedrive. i need to upload automatically some files from my linux server to onedrive, so i want to do this with php.

the tutorial is great and it works for me. But know i don't know, how i can upload files to onedrive.

please can you helpt me

my scirpt is the same like this:

https://learn.microsoft.com/en-us/graph/tutorials/php?tabs=aad&tutorial-step=7

<?php
// Enable loading of Composer dependencies
require_once realpath(__DIR__ . '/vendor/autoload.php');

require_once 'GraphHelper.php';

print('PHP Graph Tutorial'.PHP_EOL.PHP_EOL);

// Load .env file
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();
$dotenv->required(['CLIENT_ID', 'CLIENT_SECRET', 'TENANT_ID']);

initializeGraph();



$choice = $_GET["auswahl"];

    echo('Please choose one of the following options:'.PHP_EOL);
    echo('0. Exit'.PHP_EOL);
    echo('1. Display access token'.PHP_EOL);
    echo('2. List users'.PHP_EOL);
    echo('3. Make a Graph call'.PHP_EOL);

 

    switch ($choice) {
        case 1:
            displayAccessToken();
            break;
        case 2:
            listUsers();
            break;
        case 3:
            makeGraphCall();
            break;
		case 4:
       // for testing
		  
            break;
        case 0:
        default:
            print('Goodbye...'.PHP_EOL);
    }


function initializeGraph(): void {
    // TODO
	GraphHelper::initializeGraphForAppOnlyAuth();
}

function displayAccessToken(): void {
    try {
        $token = GraphHelper::getAppOnlyToken();
		var_dump ($token);
        print('App-only token: '.$token.PHP_EOL.PHP_EOL);
    } catch (Exception $e) {
        print('Error getting access token: '.$e->getMessage().PHP_EOL.PHP_EOL);
    }
}

function listUsers(): void {
    try {
        $users = GraphHelper::getUsers();

        // Output each user's details
        foreach ($users->getValue() as $user) {
            print('User: '.$user->getDisplayName().PHP_EOL);
            print('  ID: '.$user->getId().PHP_EOL);
            $email = $user->getMail();
            $email = isset($email) ? $email : 'NO EMAIL';
            print('  Email: '.$email.PHP_EOL);
        }

        $nextLink = $users->getOdataNextLink();
        $moreAvailable = isset($nextLink) && $nextLink != '' ? 'True' : 'False';
        print(PHP_EOL.'More users available? '.$moreAvailable.PHP_EOL.PHP_EOL);
    } catch (Exception $e) {
        print(PHP_EOL.'Error getting users: '.$e->getMessage().PHP_EOL.PHP_EOL);
    }
}

function makeGraphCall(): void {
	 try {
     GraphHelper::makeGraphCall();
    } catch (Exception $e) {
        print(PHP_EOL.'Error making Graph call'.PHP_EOL.PHP_EOL);
    }
}


?>


thank you

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,732 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Sourabh Gupta 795 Reputation points Microsoft Vendor
    2024-05-04T11:36:46.85+00:00

    Hi Andreas Rottmann,

    Thanks for reaching out.

    You can refer to the following link in order to upload a file with size up to 250 MB

    https://learn.microsoft.com/en-us/graph/api/driveitem-put-content?view=graph-rest-1.0&tabs=http

    For Larger files you will get the details at the following link

    https://learn.microsoft.com/en-us/graph/api/driveitem-createuploadsession?view=graph-rest-1.0

    In order to get the corresponding PHP code, you can take help from AI engines like Chat GPT . You can ask them how to make the http calls mentioned in the link in PHP.

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    0 comments No comments

  2. Andreas Rottmann 0 Reputation points
    2024-05-07T10:16:25.7766667+00:00

    not work. i get this error

    0 comments No comments