This is a PHP package for accessing Seafile Web API.
No marketing skills whatsoever, but low rates, nearly 20 years of experience, and german work attitude.
Get in touch now: https://www.reneschmidt.de/blog/impressum/
- Open Source Cloud Storage for your teams and organizations
- Built in File Encryption, better Protecting your Privacy
- Collaboration around Files, file locking and other features make collaboration easy.
To get started with Seafile PHP SDK, you may either set up your own private Seafile server (see https://www.seafile.com/en/product/private_server/) or obtain seacloud.cc account https://seacloud.cc. Because the SDK is in its infancy it's highly recommended to set up a test server or create a test account.
It's not advisable yet to use your real server/account if you already got one.
After you have created your test account continue to the next step.
mkdir ~/.seafile-php-sdk
curl -d "username=you@example.com&password=123456" https://your.seafile-server.com/api2/auth-token/ > ~/.seafile-php-sdk/api-token.json
Insert your test user name and test user password. Hint: if user name contains a "+" char, replace the char with "%2B" (hex ascii for "+") or urlencode()
the user name altogether. Just so you know.
The file ~/.seafile-php-sdk/api-token.json
will look something like this:
{"token": "your_api_token"}
The example script will assume a config file ~/.seafile-php-sdk/cfg.json
that looks like this:
{
"baseUri": "https://your-seafile-server.example.com",
"testLibId": "test-library-id",
"testLibPassword": "test-library-password"
}
The recommended way to install seafile-php-sdk is through Composer.
# Install Composer
curl -sS https://getcomposer.org/installer | php
Next, run the Composer command to install the latest stable version of seafile-php-sdk:
composer.phar require rsd/seafile-php-sdk
composer.phar dump-autoload -o # currently this is necessary
After installing, you need to require Composer's autoloader:
require 'vendor/autoload.php';
You can then later update seafile-php-sdk using composer:
composer.phar update
composer.phar dump-autoload -o # currently this is necessary
Hint: Have a look at bin/example.php
-- everything this SDK can do is covered there!
First, you need to include the API token (see above):
$tokenFile = getenv("HOME") . "/.seafile-php-sdk/api-token.json";
$token = json_decode(file_get_contents($tokenFile));
$client = new Client(
[
'base_uri' => 'https://your.seafile-server.com',
'debug' => false,
'headers' => [
'Authorization' => 'Token ' . $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);
foreach ($items as $item) {
printf("%s: %s (%d bytes)\n", $item->type, $item->name, $item->size);
}
$parentDir = '/'; // Directory must exist within this folder
$directory = 'DirectoryName';
if($directoryResource->exists($lib, $directory, $parentDir) === false) {
// directory does not exist
}
Because Seafile Web API don't provide a function to do this check, the library load all child items of $parent_dir and checks manually.
$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);
Trying to download a file from an encrypted library without unlocking it first would inevitably fail, so just unlock (API docs say "decrypt") the library before attempting:
$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 = json_decode((string)$response->getBody());
$response = $fileResource->update($lib, $newFilename, '/');
$updatedFileId = json_decode((string)$response->getBody());
$directoryItem = $fileResource->getFileDetail($lib, '/' . basename($fullFilePath));
This example requires monolog. Log entries and Guzzle debug info will be written to stdout.
$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.com',
'debug' => true,
'handler' => $stack,
'headers' => [
'Authorization' => 'Token ' . $token->token
]
]
);
- Please let me know of issues.
- PHP >=5.5
- Guzzle 6
Please note that this package still is in its infancy. Only a small part of the API has been implemented so far.
Pull requests are welcome.
- http://seafile.com
- https://www.seafile-server.org/ (Seafile server hosting in Germany)
- http://manual.seafile.com/develop/web_api.html#seafile-web-api-v2
- https://reneschmidt.de
MIT © 2015 Rene Schmidt DevOps UG (haftungsbeschränkt) & Co. KG