PHP code example of gwsn / sharepoint-sdk

1. Go to this page and download the library: Download gwsn/sharepoint-sdk 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/ */

    

gwsn / sharepoint-sdk example snippets

 php
    // Initialize the drive
    $spDrive = new DriveService($accessToken);
    $driveId = $spDrive->requestDriveId($siteId);
    $spDrive->setDriveId($driveId);
    
     // Check if Resource exists
    try {
        $result = $spDrive->checkResourceExists('/test');
        var_dump($result);
        $result = $spDrive->checkResourceExists('/testDoc.docx');
        var_dump($result);
    } catch (\Exception $exception) {
        var_dump($exception->getMessage());
    }

    // move file /test.txt to folder /test and rename it to testje.txt
    $result = $spFileService->moveFile('/test.txt', '/test', 'testje.txt');
    
    // check if it still exists
    $result = $spDrive->checkResourceExists('/test.txt');
    var_dump($result);
    
    // check if new file exists
    $result = $spDrive->checkResourceExists('/test/testje.txt');
    var_dump($result);
    
 php
    // FileService
    $spFileService = new FileService($accessToken, $driveId);

    // write file to directory
    $result = $spFileService->writeFile('/test.txt', 'testContent');
    var_dump(isset($result['id']));

    // read file from directory
    $content = $spFileService->readFile('/test.txt');
    var_dump(($content === 'testContent'));

    // write file to directory
    $result = $spFileService->writeFile('/test/docje.txt', 'testContent');
    var_dump(isset($result['id']));

    // read file from directory
    $content = $spFileService->readFile('/test/docje.txt');
    var_dump(($content === 'testContent'));
   

    // move file
    $result = $spFileService->moveFile('/test.txt', '/test', 'testje.txt');
    $result = $spDrive->checkResourceExists('/test.txt');
    var_dump($result);
    $result = $spDrive->checkResourceExists('/test/testje.txt');
    var_dump($result);

    // copy file
    $result = $spFileService->copyFile('/test/testje.txt', '/', 'toBeDeleted.txt');
    var_dump($result);
    // copy dir
    $result = $spFileService->copyFile('/test', '/test2');
    var_dump($result);


    // delete file from directory
    $content = $spFileService->deleteFile('/test/testje.txt');
    var_dump($content);
    $content = $spFileService->deleteFile('/test/docje.txt');
    var_dump($content);
    $content = $spFileService->deleteFile('/toBeDeleted.txt');
    var_dump($content);
    $content = $spFileService->deleteFile('/test.txt');
    var_dump($content);