PHP code example of cyhello / seafile-php-sdk

1. Go to this page and download the library: Download cyhello/seafile-php-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/ */

    

cyhello / seafile-php-sdk example snippets




$client = new Client(
    [
        'base_uri' => 'https://your-seafile-server.example.com',
        'debug' => false,
        'headers' => [
            'Authorization' => 'Token ' . $token
        ]
    ]
);

$libraryResource = new Library($client);
$libs = $libraryResource->getAll();

foreach ($libs as $lib) {
    printf("Name: %s, ID: %s, is encrypted: %s\n", $lib->name, $lib->id, $lib->encrypted ? 'YES' : 'NO');
}

$directoryResource = new Directory($client);
$lib = $libraryResource->getById('some library ID of yours');
$items = $directoryResource->getAll($lib, '/'); // 2nd param is the name of the directory or '/' 

foreach ($items as $item) {
    printf("%s: %s (%d bytes)\n", $item->type, $item->name, $item->size);
}

$parentDir = '/'; // DirectoryItem must exist within this directory
$directory = 'DirectoryName';
if($directoryResource->exists($lib, $directoryItemName, $parentDir) === false) {
 //  directory item does not exist
}

$parentDir = '/'; // Create directory within this folder
$directory = 'DirectoryName'; // name of the new Directory
$recursive = false; // recursive will create parentDir if not already existing
$success = $directoryResource->create($lib, $directory, $parentDir, $recursive);

$dir = '/'; // dir in the library
$saveTo = '/tmp/'. $item->name; // save file to this local path
$fileResource = new File($client);
$downloadResponse = $fileResource->downloadFromDir($lib, $item, $saveTo, $dir);

$success = $libraryResource->decrypt($libId, ['query' => ['password' => $password]]);
// rest is the same as 'Download file from unencrypted library', see above


$fileToUpload = '/path/to/file/to/be/uploaded.zip';
$dir = '/'; // directory in the library to save the file in
$response = $fileResource->upload($lib, $fileToUpload, $dir);
$uploadedFileId = (string)$response->getBody();

$response = $fileResource->update($lib, $newFilename, '/');
$updatedFileId = (string)$response->getBody();

$directoryItem = $fileResource->getFileDetail($lib, '/' . basename($fullFilePath));

$accountResource = new Account($client);

$accountType = $accountResource->getInfo();

print_r($accountType->toArray());

$accountResource = new Account($client);

$accountTypes = $accountResource->getAll();

foreach ($accountTypes as $accountType) {
    print_r($accountType->toArray());
}

$newAccountType = (new AccountType)->fromArray([
    'email' => '[email protected]',
    'password' => 'password',
    'name' => 'Hugh Jazz',
    'note' => 'I will not waste chalk',
    'institution' => 'Duff Beer Inc.'
]);

$success = $accountResource->create($newAccountType);

$updateAccountType = (new AccountType)->fromArray([
    'name' => 'Divine Hugh Jazz',
    'email' => '[email protected]'
]);

$success = $accountResource->update($updateAccountType);

$accountResource = new Account($client);

$accountType = $accountResource->getByEmail('[email protected]');

print_r($accountType->toArray());

$accountResource = new Account($client);

$accountType = (new AccountType)->fromArray([
    'email' => '[email protected]'
]);

$success = $accountResource->remove($accountType);

$accountResource = new Account($client);

$success = $accountResource->removeByEmail('[email protected]');

$accountType = (new AccountType)->fromArray([
   'email' => '[email protected]'
]);

$avatarResource = new Avatar($client);

print_r($avatarResource->getUserAvatar($accountType)->toArray());

print_r($avatarResource->getUserAvatarByEmail('[email protected]')->toArray());

$libraryResource = new Library($client);
$directoryResource = new Directory($client);
$fileResource = new File($client);
$shareLinkResource = new ShareLinks($client);

// create share link for a file
$expire = 5;
$p = "/" . basename($newFilename);
$password = 'qwertz123';

$defaultPermissions = new SharedLinkPermissions(SharedLinkPermissions::CAN_DOWNLOAD);
$extendedPermissions = new SharedLinkPermissions(SharedLinkPermissions::CAN_DOWNLOAD | SharedLinkPermissions::CAN_EDIT);

$shareLinkType = $shareLinkResource->create($lib, $p, $defaultPermissions, $expire, $password);

// remove shared link
$success = $shareLinkResource->remove($shareLinkType);

$libraryResource = new Library($client);
$starredFileResource = new StarredFile($client);

// get all starred files
$dirItems = $starredFileResource->getAll();

// unstar all starred files
foreach ($dirItems as $dirItem) {
    $lib = $libraryResource->getById($dirItem->repo);
    $starredFileResource->unstar($lib, $dirItem);
}

// re-star all files
foreach ($dirItems as $dirItem) {
    $lib = $libraryResource->getById($dirItem->repo);
    $starredFileResource->star($lib, $dirItem);
}

$logger = new Logger('Logger');

$stack = HandlerStack::create();
$stack->push(
    Middleware::log(
        $logger,
        new MessageFormatter("{hostname} {req_header_Authorization} - {req_header_User-Agent} - [{date_common_log}] \"{method} {host}{target} HTTP/{version}\" {code} {res_header_Content-Length} req_body: {req_body} response_body: {res_body}")
    )
);

$client = new Client(
    [
        'base_uri' => 'https://your-seafile-server.example.com',
        'debug' => true,
        'handler' => $stack,
        'headers' => [
            'Authorization' => 'Token ' . $token
        ]
    ]
);
bash
# Install Composer
curl -sS https://getcomposer.org/installer | php
bin/example.php