The Files.com PHP SDK provides convenient Files.com API access to applications written in PHP.
Install Composer. See https://packagist.org for more info.
If composer.phar
is already available, skip this step.
curl -sS https://getcomposer.org/installer | php
Install the SDK
php composer.phar require files.com/files-php-sdk
- PHP 5.5+
- php-curl extension
require 'vendor/autoload.php';
// set client to use a mock Files.com server for testing
\Files\Files::setBaseUrl('https://MY-SUBDOMAIN.files.com');
There are multiple ways to authenticate to the API.
You can set an API key globally like this:
\Files\Files::setApiKey('my-api-key');
Or, you can pass an API key per-request, in the options array at the end of every method like this:
$user = new \Files\Model\User($params, array('api_key' => 'my-api-key'));
Or, you can open a user session by calling \Files\Model\Session::create()
$session = \Files\Model\Session::create(['username' => $username, 'password' => $password]);
Then use it globally for all subsequent API calls like this:
\Files\Files::setSessionId($session->id);
Or, you can pass the session ID per-request, in the options array at the end of every method like this:
$user = new \Files\Model\User($params, array('session_id' => $session->id));
$session = \Files\Model\Session::create(['username' => $myUsername, 'password' => $myPassword]);
\Files\Files::setSessionId($session->id);
// do something
\Files\Model\ApiKey::all(['user_id' => 0]);
// clean up when done
\Files\Model\Session::destroy();
\Files\Files::setSessionId(null);
You can set the following global properties directly on the \Files\Files
class:
\Files\Files::$logLevel
- set to one of the following:\Files\LogLevel::NONE
\Files\LogLevel::ERROR
\Files\LogLevel::WARN
\Files\LogLevel::INFO
(default)\Files\LogLevel::DEBUG
\Files\Files::$debugRequest
- enable debug logging of API requests (default:false
)\Files\Files::$debugResponseHeaders
- enable debug logging of API response headers (default:false
)\Files\Files::$connectTimeout
- network connect timeout in seconds (default:30.0
)\Files\Files::$readTimeout
- network read timeout in seconds (default:90.0
)\Files\Files::$maxNetworkRetries
- max retries (default:3
)\Files\Files::$minNetworkRetryDelay
- minimum delay in seconds before retrying (default:0.5
)\Files\Files::$maxNetworkRetryDelay
- max delay in seconds before retrying (default:1.5
)\Files\Files::$autoPaginate
- auto-fetch all pages when results span multiple pages (default:true
)
$rootFiles = \Files\Model\Folder::listFor('/');
\Files\Model\File::uploadFile($destinationFileName, $sourceFilePath);
\Files\Model\File::uploadData($destinationFileName, $fileData);
\Files\Model\File::downloadToStream($remoteFilePath, $outputStream);
// download entire file - with retries enabled
\Files\Model\File::downloadToFile($remoteFilePath, $localFilePath);
// partially download - just the first KB
\Files\Model\File::partialDownloadToFile($remoteFilePath, $localFilePath, 0, 1023);
// resume an incomplete download
\Files\Model\File::resumeDownloadToFile($remoteFilePath, $localFilePath);
$foundFile = \Files\Model\File::find($remoteFilePath);
$file = new \Files\Model\File();
$file->get($remoteFilePath);
$file->update([
'provided_mtime' => '2000-01-01T01:00:00Z',
'priority_color' => 'red',
]);
$file->metadata([
'with_previews' => true,
'with_priority_color' => true,
]);
For related documentation see Case Sensitivity Documentation.
if(\Files\Util\PathUtil::same("Fïłèńämê.Txt", "filename.txt")) {
echo "Paths are the same\n";
}
Additional docs are available at https://developers.files.com
In Version 2.0, the Files.com PHP SDK was updated to comply with both the PSR-12 coding standard and the PSR-4 autoloading standard. No new classes were added or any exising classes removed, but some where moved to comply with the PSR-4 standard. If a client of the sdk references the moved classes, the client code will need to be updated to reference the new location of these classes.
The affected classes where primarly Exception classes. Exceptions where moved into their own namespace (and source files).
The following table shows the classes that where changed for compliance
The Base exception were moved from the \Files
namespace to the \Files\Exception
namespace.
Examples of Base Exceptions Classes moved.
SDK < 2.0 Class Location | SDK >= 2.0 Class Location |
---|---|
\Files\ApiException |
Files\Exception\ApiException |
\Files\FilesException |
Files\Exception\FilesException |
\Files\ConfigurationException |
Files\Exception\ConfigurationException |
The BadRequest group of exceptions were moved from the \Files\BadRequest
namespace to the \Files\Exception\BadRequest
namespace.
Example of BadRequest Classes moved.
SDK < 2.0 Class Location | SDK >= 2.0 Class Location |
---|---|
\Files\BadRequest\AgentUpgradeRequiredException |
Files\Exception\BadRequest\AgentUpgradeRequiredException |
The NotAuthenticated group of exceptions were moved from the \Files\NotAuthenticated
namespace to the \Files\Exception\NotAuthenticated
namespace.
Example of NotAuthenticated Classes moved.
SDK < 2.0 Class Location | SDK >= 2.0 Class Location |
---|---|
\Files\NotAuthenticated\AdditionalAuthenticationRequiredException |
Files\Exception\NotAuthenticated\AdditionalAuthenticationRequiredException |
The NotAuthorized group of exceptions were moved from the \Files\NotAuthorized
namespace to the \Files\Exception\NotAuthorized
namespace.
Example of NotAuthorized Classes moved.
SDK < 2.0 Class Location | SDK >= 2.0 Class Location |
---|---|
\Files\NotAuthorized\ApiKeyIsDisabledException |
Files\Exception\NotAuthorized\ApiKeyIsDisabledException |
The NotFound group of exceptions were moved from the \Files\NotFound
namespace to the \Files\Exception\NotFound
namespace.
Example of NotFound Classes moved.
SDK < 2.0 Class Location | SDK >= 2.0 Class Location |
---|---|
\Files\NotFound\ApiKeyNotFoundException |
Files\Exception\NotFound\ApiKeyNotFoundException |
The ProcessingFailure group of exceptions were moved from the \Files\ProcessingFailure
namespace to the \Files\Exception\ProcessingFailure
namespace.
Example of ProcessingFailure Classes moved.
SDK < 2.0 Class Location | SDK >= 2.0 Class Location |
---|---|
\Files\ProcessingFailure\AlreadyCompletedException |
Files\Exception\ProcessingFailure\AlreadyCompletedException |
The ProcessingFailure group of exceptions were moved from the \Files\RateLimited
namespace to the \Files\Exception\RateLimited
namespace.
Example of RateLimited Classes moved.
SDK < 2.0 Class Location | SDK >= 2.0 Class Location |
---|---|
\Files\RateLimited\DuplicateShareRecipientException |
Files\Exception\RateLimited\DuplicateShareRecipientException |
The ServiceUnavailable group of exceptions were moved from the \Files\ServiceUnavailable
namespace to the \Files\Exception\ServiceUnavailable
namespace.
Example of ServiceUnavailable Classes moved.
SDK < 2.0 Class Location | SDK >= 2.0 Class Location |
---|---|
\Files\ServiceUnavailable\AgentUnavailableException |
Files\Exception\ServiceUnavailable\AgentUnavailableException |
The SiteConfiguration group of exceptions were moved from the \Files\SiteConfiguration
namespace to the \Files\Exception\SiteConfiguration
namespace.
Example of SiteConfiguration Classes moved.
SDK < 2.0 Class Location | SDK >= 2.0 Class Location |
---|---|
\Files\SiteConfiguration\AccountAlreadyExistsException |
Files\Exception\SiteConfiguration\AccountAlreadyExistsException |
The Files.com team is happy to help with any SDK Integration challenges you may face.
Just email support@files.com and we'll get the process started.