使用PHP访问WebDAV

问题描述 投票:0回答:1

我有以下代码:

<?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);

输出:

致命错误:未捕获的Sabre \ HTTP \ ClientHttpException:方法不正确允许进入/var/www/html/lib/DAV/Client.php:235堆栈跟踪:#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

在服务器端,我安装了nextcloud实例(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 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" 
}
php webdav nextcloud
1个回答
0
投票

当您呼叫$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);
© www.soinside.com 2019 - 2024. All rights reserved.