Categories
Cloud storage PHP

How to access Nextcloud using WebDAV and PHP

A little over a month ago, I discovered Nextcloud. An easy to set up, open-source and self-hosted alternative cloud storage solution (that can actually do a lot more than sync files). After testing it for a bit, I decided to switch from Dropbox to a Nextcloud instance that’s hosted on an everyday web server. However, I am running an application that syncs data to Dropbox (photos mostly, bust also log files) via the Dropbox API. In order to fully switch to Nextcloud I’d have to figure out how to sync this data to Nextcloud instead. I did not want to write my own PHP client from scratch so I started looking for a somewhat simple, plug-and-playish solution. Here’s what I found.

Prepare Nextcloud

We will be using WebDAV to access Nextcloud, so you will need the following:

  • The WebDAV base url for your instance (e.g. https://nextcloud.example.com/remote.php/dav)
  • Your username
  • Your password

I highly recommend using an app password for this rather than your actual account password. You can create one by going to Settings > Security > New app password.

Prepare PHP

While there is a WebDAV client package for PHP, I ended up using a framework called sabre/dav. A framework is much easier to implement and works on every normal web server (which usually won’t allow you to install any PHP component you want). In order to install the framework, simply download composer and run:

composer require sabre/dav

This will download the framework and all required dependencies for you. If you don’t know how to use composer, check out the intro on the official website.

Write PHP

Let’s get startet on our PHP code that will access Nextcloud. Before we can start, we will neet to “import” the framework into our script:

<?php

include 'vendor/autoload.php';

Once again, composer handles everything. We only need to include it’s autoloader. Now, we need to add our Nextcloud details.

<?php

include 'vendor/autoload.php';

$settings = array(
    'baseUri' => 'https://nextcloud.example.com/remote.php/dav',
    'userName' => 'yourusername',
    'password' => 'yourpassword'
);

Please note: It is a pretty bad idea to store your password in plain text like this (even if it is an app password). You should look into storing it more safely if you are planning to run this long-term (e.g. encrypting your password).

We can now create a new Sabre Client with the above details.

<?php

include 'vendor/autoload.php';

$settings = array(
    'baseUri' => 'https://nextcloud.example.com/remote.php/dav',
    'userName' => 'yourusername',
    'password' => 'yourpassword'
);

$client = new Sabre\DAV\Client($settings);

That’s all the setup we need. We can now use the different WebDAV methods in order to access Nextcloud. Here are a few examples:

<?php

include 'vendor/autoload.php';

$settings = array(
    'baseUri' => 'https://nextcloud.example.com/remote.php/dav',
    'userName' => 'yourusername',
    'password' => 'yourpassword'
);

$client = new Sabre\DAV\Client($settings);

// Upload a file
$upload_result = $client->request('PUT', 'test-upload.txt', 'This will be written to the txt file');

// List a folder's contents
$folder_content = $client->propFind('path/to/folder', array(
    '{DAV:}getlastmodified',
    '{DAV:}getcontenttype',
), 1);

You can find a few more examples in the sabre/dav documentation on the WebDAV client.

This should help you get started with accessing your Nextcloud account using PHP. I highly recommend trying the different methods and simply do a var_dump() on the response in order to understand the structure of the return values. A look at the source code can also help understanding how the client works and behaves. If you have any feedback or questions, feel free to hit me up on twitter.


Sources