PHP code example of ssswang / sharepoint-graph-client

1. Go to this page and download the library: Download ssswang/sharepoint-graph-client library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ssswang / sharepoint-graph-client example snippets


use SharepointGraphClient\GraphSite;

$site = GraphSite::create('https://contoso.sharepoint.com/sites/team', [
    'site' => [
        'tenant'    => '11111111-2222-3333-4444-555555555555',   // Azure AD Tenant ID (GUID)
        'client_id' => '00000000-0000-0000-0000-000000000000',
        'secret'    => 'your-client-secret',

        // ...or certificate credentials instead of a secret:
        // 'certificate' => [
        //     'private_key' => '/path/to/key.pem',  // or the PEM contents
        //     'thumbprint'  => 'base64url-sha1-thumbprint', // (x5t)
        // ],
    ],
]);

$site->setGraphAccessToken(new GraphAccessToken([
    'access_token' => 'eyJ0eXAi...',
    'expires_in'   => 3600,
]));

use SharepointGraphClient\GraphList;

// all lists of the site (allowed template types only)
$lists = GraphList::getAll($site);

// by title or GUID
$list = GraphList::getByTitle($site, 'Projects');
$list = GraphList::getByGUID($site, '00000000-0000-0000-0000-000000000000');

// items (paginated automatically)
$items = $list->getGraphItems();

$item = $list->getGraphItem('42');
$item = GraphItem::getByTitle($list, 'My entry');

// CRUD
$created  = $list->createGraphItem(['Title' => 'New entry', 'Status' => 'Open']);
$created->update(['Status' => 'Done']);
$created->delete();

// columns/fields
$columnId = $list->createGraphColumn([
    'name' => 'Status',
    'choice' => ['choices' => ['Open', 'Done']],
]);
$columns = $list->getGraphColumns();

$drive = $site->getDefaultGraphDrive();           // or getGraphDriveByName('Documents')
$root  = $drive->getRootGraphFolder();
$docs  = $drive->getGraphFolderByPath('Shared Docs');

$sub = GraphFolder::create($root, 'Archive');
$files = $docs->getGraphItems();                  // folders and files, keyed by item ID

// upload (simple, up to 250 MB)
$file = GraphFile::create($docs, 'hello world', 'hello.txt', overwrite: true);
$file = GraphFile::create($docs, new SplFileInfo('/path/to/report.pdf'));

// upload large files with a resumable upload session (chunked)
$file = GraphFile::createResumable($docs, new SplFileInfo('/path/to/huge.zip'));

// or drive the session manually (replaces startUpload/continueUpload/finishUpload)
$session = GraphUploadSession::create($docs, 'huge.zip', overwrite: true);
$session->uploadChunk($chunk1, offset: 0, totalSize: 10485760);
$session->uploadChunk($chunk2, offset: 5242880, totalSize: 10485760);
// ...the final chunk returns the GraphFile

// download
$contents = $file->getContents();

// update, move, copy, delete
$file->update('new contents');
$file->move($sub, 'renamed.txt');                 // same drive only (Graph limitation)
$copied = $file->copy($sub);                      // async operation, monitored automatically
$file->delete();

// the underlying list item of a file
$listItem = $file->getGraphListItem();

use SharepointGraphClient\GraphUser;

$user = GraphUser::getByAccount($site, '[email protected]');
$user = GraphUser::getByEmail($site, '[email protected]');
$all  = GraphUser::getAll($site);

// current user (
sh
php examples/upload-test.php \
  --tenant 11111111-2222-3333-4444-555555555555 \
  --client-id 00000000-0000-0000-0000-000000000000 \
  --client-secret "your-client-secret" \
  --domain contoso.sharepoint.com \
  --site sites/team \
  --folder "Graph Client Test" \
  --cleanup
ini
curl.cainfo = "C:/path/to/php/cacert.pem"
openssl.cafile = "C:/path/to/php/cacert.pem"