使用 PHP Curl 从 webDAV 站点下载文件

Downloading a file from a webDAV site with PHP Curl

我需要使用 PHP 从 webDAV 站点下载文件。我已经使用 cURL 尝试了下面的代码,但出现“400 Bad Request”错误。

有人可以帮我解决我做错的事吗?

$fp = fopen("c:/downloadtest.text", "w");

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$URL);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 30 seconds
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");

$result = curl_exec ($ch);

curl_close ($ch);

fclose($fp);

我终于让它工作了,我不确定如何让 CURLOPT_WRITEFUNCTION 选项工作:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);

$output = curl_exec($ch);

$fp = fopen("c:/downloadtest.text", "w");
fwrite($fp, $output);
fclose($fp);

curl_close($ch);