使用 PHP 访问 WebDAV

Access WebDAV using PHP

我有以下代码:

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

$settings = array(
    'baseUri' => "https://example.com/remote.php/dav/files/foo/",
    'userName' => "foo",
    'password' => "secret"
);

$client = new Client($settings);
$response = $client->request('GET');
var_dump($response);

这是输出:

array(3) {
  ["body"]=>
  string(114) "This is the WebDAV interface. It can only be accessed by WebDAV clients such as the Nextcloud desktop sync client."
  ["statusCode"]=>
  int(200)
  ["headers"]=>
  array(19) {
    ["date"]=>
    array(1) {
      [0]=>
      string(29) "Fri, 13 Dec 2019 09:44:50 GMT"
    }
    ["server"]=>
    array(1) {
      [0]=>
      string(22) "Apache/2.2.15 (CentOS)"
    }
    ["strict-transport-security"]=>
    array(1) {
      [0]=>
      string(44) "max-age=15768000; includeSubDomains; preload"
    }
    ["x-powered-by"]=>
    array(1) {
      [0]=>
      string(10) "PHP/7.1.33"
    }
    ["set-cookie"]=>
    array(6) {
      [0]=>
      string(76) "ocyvic3wn5tf=c7ca073dcacf6c271602a08aa0687614; path=/files; secure; HttpOnly"
      [1]=>
      string(186) "oc_sessionPassphrase=rR6PwI4AOETBOmb3av8PDUbODPYeoffuBZE%2BAoi9PHu7OYoACFD%2BMMZal4ckRe8TiXf7i3knRn4FJwwFrRuXsaQmHqE6PV0TQNookMRBDQkcO06xg8vZwLc005u%2BajUy; path=/files; secure; HttpOnly"
      [2]=>
      string(108) "nc_sameSiteCookielax=true; path=/files; httponly;secure; expires=Fri, 31-Dec-2100 23:59:59 GMT; SameSite=lax"
      [3]=>
      string(114) "nc_sameSiteCookiestrict=true; path=/files; httponly;secure; expires=Fri, 31-Dec-2100 23:59:59 GMT; SameSite=strict"
      [4]=>
      string(76) "ocyvic3wn5tf=5ffdd5c7c6d207d05cde8bc5b3caeacd; path=/files; secure; HttpOnly"
      [5]=>
      string(69) "cookie_test=test; expires=Fri, 13-Dec-2019 10:44:50 GMT; Max-Age=3600"
    }
    ["expires"]=>
    array(1) {
      [0]=>
      string(29) "Thu, 19 Nov 1981 08:52:00 GMT"
    }
    ["cache-control"]=>
    array(1) {
      [0]=>
      string(35) "no-store, no-cache, must-revalidate"
    }
    ["pragma"]=>
    array(1) {
      [0]=>
      string(8) "no-cache"
    }
    ["content-security-policy"]=>
    array(1) {
      [0]=>
      string(19) "default-src 'none';"
    }
    ["referrer-policy"]=>
    array(1) {
      [0]=>
      string(11) "no-referrer"
    }
    ["x-content-type-options"]=>
    array(1) {
      [0]=>
      string(7) "nosniff"
    }
    ["x-download-options"]=>
    array(1) {
      [0]=>
      string(6) "noopen"
    }
    ["x-frame-options"]=>
    array(1) {
      [0]=>
      string(10) "SAMEORIGIN"
    }
    ["x-permitted-cross-domain-policies"]=>
    array(1) {
      [0]=>
      string(4) "none"
    }
    ["x-robots-tag"]=>
    array(1) {
      [0]=>
      string(4) "none"
    }
    ["x-xss-protection"]=>
    array(1) {
      [0]=>
      string(13) "1; mode=block"
    }
    ["content-length"]=>
    array(1) {
      [0]=>
      string(3) "114"
    }
    ["connection"]=>
    array(1) {
      [0]=>
      string(5) "close"
    }
    ["content-type"]=>
    array(1) {
      [0]=>
      string(24) "text/html; charset=UTF-8"
    }
  }
}

如果我这样做

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

$settings = array(
    'baseUri' => "https://example.com/remote.php/dav/files/foo/",
    'userName' => "foo",
    'password' => "secret"
);

$client = new Client($settings);
$folder_content = $client->propFind('/', array(
    '{DAV:}getlastmodified',
    '{DAV:}getcontenttype',
),1);
var_dump($folder_content);

输出:

Fatal error: Uncaught Sabre\HTTP\ClientHttpException: Method Not Allowed in /var/www/html/lib/DAV/Client.php:235 Stack trace: #0 /var/www/html/index.php(23): Sabre\DAV\Client->propFind('https://example...', Array, 1) #1 {main} thrown in /var/www/html/lib/DAV/Client.php on line 235

我在 webDAV 中使用以下库进行访问:https://github.com/sabre-io/dav

在服务器端我安装了一个 nextcloud 实例 (v 17)。

基于此 post https://medium.com/@cetteup/how-to-access-nextcloud-using-webdav-and-php-2c00a04e35b9

在 PHP 中是否有任何其他方法可以通过 webDAV 列出文件夹的内容?

编辑:

<?php
include 'vendor/autoload.php';
$settings = array(
    'baseUri' => "https://example.com/remote.php/dav/files/foo/",
    'userName' => "foo",
    'password' => "secret"
);
$client = new Sabre\DAV\Client($settings);
$features = $client->options();
var_dump($features);

输出:

array(3) { 
    [0]=> string(1) "1" 
    [1]=> string(1) "3" 
    [2]=> string(14) "extended-mkcol" 
}

当您调用 $client->propFind('/', ....) 时,实际有效的 url 将是:

https://example.com/

而不是

https://example.com/remote.php/dav/files/foo/

要获取与基本 uri 相关的文件夹的内容,您只需删除斜杠

$folder_content = $client->propFind('', array(
    '{DAV:}getlastmodified',
    '{DAV:}getcontenttype',
),1);