You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows11下PHP用sabre/dav连接NextCloud遇SSL证书问题求助

PHP连接NextCloud时SSL证书验证失败的解决方法

问题背景

在Windows 11系统中使用PHP通过sabre/dav库连接NextCloud,已通过Composer成功安装sabre/dav,编写的文件列表代码如下:

<?php

// installed via - composer require sabre/dav

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

$settings = array(
    'baseUri' => 'https://cd.wedos.com/remote.php/dav/files/wedos-auth-xxxxxxxx',
    'userName' => 'myEmail@gmail.com',
    'password' => 'myPasssword'
);

$client = new 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);

运行时抛出SSL证书错误:

PHP Fatal error:  Uncaught Sabre\HTTP\ClientException: SSL certificate problem: unable to get local issuer certificate in D:\data\projects\erp\php\vendor\sabre\http\lib\Client.php:327

执行curl -v cd.wedos.com验证显示证书本身无问题(证书由Let's Encrypt R3签发,验证通过),推测问题出在PHP未正确加载系统或指定的CA证书。

两种解决方法

1. 临时禁用SSL证书验证(仅测试/内部环境使用)

修改sabre/dav的客户端配置,添加curl选项关闭证书验证:

$settings = array(
    'baseUri' => 'https://cd.wedos.com/remote.php/dav/files/wedos-auth-xxxxxxxx',
    'userName' => 'myEmail@gmail.com',
    'password' => 'myPasssword',
    // 添加以下curl配置
    'curlOptions' => array(
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_SSL_VERIFYHOST => 0,
    )
);

注意:此方法会跳过证书验证,存在中间人攻击风险,仅建议在完全可信的内部环境临时使用。

2. 配置PHP信任Let's Encrypt证书(推荐)

步骤如下:

  1. 下载Let's Encrypt根证书:
    需要下载ISRG Root X1根证书(因为R3中间证书由它签发),保存为cacert.pem文件,可直接保存到PHP安装目录下的extras/ssl文件夹(比如D:\php\extras\ssl\cacert.pem)。

  2. 修改php.ini配置:
    找到PHP的php.ini文件(可通过php --ini命令查看路径),取消以下两行的注释并修改为证书文件的绝对路径:

    curl.cainfo = "D:\php\extras\ssl\cacert.pem"
    openssl.cafile = "D:\php\extras\ssl\cacert.pem"
    

    注意Windows路径使用反斜杠,或者用转义的正斜杠(D:/php/extras/ssl/cacert.pem)。

  3. 重启PHP服务:
    如果是Web环境(如Apache、Nginx),重启对应的服务;如果是CLI环境,重新打开命令行窗口即可。

  4. 验证配置生效:
    运行以下PHP代码检查证书路径是否正确加载:

    <?php
    var_dump(openssl_get_cert_locations());
    ?>
    

    输出中cafile字段应显示你配置的cacert.pem路径。

内容的提问来源于stack exchange,提问作者Radek

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 20:55:54