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

时间:2017-10-26 20:43:04

标签: php webdav

我需要使用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);

1 个答案:

答案 0 :(得分:1)

我终于让它工作了,我不知道如何让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);