使用 PHP 访问 WebDAV



我有以下代码:

<?php
include 'vendor/autoload.php';
use SabreDAVClient;
$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 SabreDAVClient;
$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);

输出:

致命错误: 未捕获的 Sabre\HTTP\ClientHttpException: 方法不是 允许进入/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} 扔在/var/www/html/lib/DAV/Client.php 中 235路

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

在服务器端,我安装了一个下一个云实例(v 17(。

它基于这篇文章 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 SabreDAVClient($settings);
$features = $client->options();
var_dump($features);

输出:

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

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

https://example.com/

而不是

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

要获取相对于基 uri 的文件夹内容,只需删除斜杠

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

最新更新