Articles on: API

Using the Leviia Drive API - PHP

Installing PHP and its dependencies :


To install PHP on your environment, go to: https://www.php.net/manual/fr/install.php.

Once your PHP environment is up and running (hello-world.php is up and running), install php-curl and SabreDAV (WebDAV client for PHP). Here's an example, to be adapted to your environment, under Linux (Debian) :

apt update
apt install php-curl
composer require sabre/dav ~3.2.0


Click here for SabreDAV documentation : https://sabre.io/dav/install/.

You can now connect your PHP code to your Leviia account.

Client creation :


To initialize login information :

use Sabre\DAV\Client;
include 'vendor/autoload.php';

$settings = array(
    'baseUri' => 'https://cloud.leviia.com/',
    'userName' => 'thomas',
    'password' => '*******',
);

$client = new Client($settings);
$base = "/remote.php/dav/files/$settings[userName]";


List the contents of a directory :


To list the contents of the root directory ("/"), use :

$response = $client->propfind($base."/", array(
    '{DAV:}displayname',
    '{DAV:}getcontentlength',
),1);

echo '<pre>'; print_r($response); echo '</pre>';


To list the contents of a subdirectory, simply replace "/" with the directory path :

$response = $client->propfind($base."/documents/", array(
    '{DAV:}displayname',
    '{DAV:}getcontentlength',
),1);

echo '<pre>'; print_r($response); echo '</pre>';


Send a file to Leviia :


$fh_res = fopen("/CHEMIN/VERS/LE/FICHIER", 'r');

$response = $client->request('PUT', $base.'/FICHIER', $fh_res);


Download a file from Leviia :


$response = $client->request('GET',$base.'/FICHIER/DISTANT');
$file = fopen('/CHEMIN/LOCAL', "w+");
fputs($file, $response["body"]);
fclose($file);


Create a folder on Leviia :


$response = $client->request('MKCOL', $base.'/CHEMIN');


Delete a folder/file on Leviia :


$response = $client->request('DELETE', $base.'/CHEMIN');

Updated on: 08/01/2024

Was this article helpful?

Share your feedback

Cancel

Thank you!