我试图下载使用PHP和卷曲的htaccess的保护目录中的文件。这是我的code:
$用户名=MyUserName输入;
$密码=MyPassword输入;
$ URL =http://www.example.com/private/file.pdf;
$ CH = curl_init();
curl_setopt($ CH,CURLOPT_URL,$网址);
curl_setopt($ CH,CURLOPT_RETURNTRANSFER,真正的);
curl_setopt($ CH,CURLOPT_USERPWD,$用户名:密码$);
curl_setopt($ CH,CURLOPT_HTTPAUTH,CURLAUTH_BASIC);
$输出= curl_exec($ CH);
$信息= curl_getinfo($ CH);
curl_close($ CH);
但是,这code什么也不做......我怎么能启动file.pdf的下载?
感谢您!
另外,如果我回声$输出,我得到这样的:
阵列([URL => http://www.example.com/private/file.pdf [内容] =>应用/ PDF [HTTP_ code ] =&并在200 [header_size] => 264 [request_size] => 116 [FILETIME] => -1 [ssl_verify_result] =大于0 [redirect_count] =大于0 [TOTAL_TIME] => 0.007898 [namelookup_time ] => 0.006777 [CONNECT_TIME] => 0.006858 [pretransfer_time] => 0.006922 [size_upload] =大于0 [size_download] => 27369 [speed_download] => 3465307 [speed_upload] =大于0 [download_content_length] => 27369 [upload_content_length] =大于0 [starttransfer_time] => 0.007839 [redirect_time] =&0)
解决方案
有一个 CURLOPT_BINARYTRANSFER
选项似乎缺少。你粘贴输出是 $资讯
不是 $输出
BTW。它显示的下载发生, download_content_length
是27369。
I tried to download files in a htaccess protected directory using php and curl. This is my code:
$username = "MyUsername";
$password = "MyPassword";
$url = "http://www.example.com/private/file.pdf";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
But this code does nothing... how can I initiate the download of file.pdf?
Thank you!
Also, if I echo $output, i get this:
Array ( [url] => http://www.example.com/private/file.pdf [content_type] => application/pdf [http_code] => 200 [header_size] => 264 [request_size] => 116 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0.007898 [namelookup_time] => 0.006777 [connect_time] => 0.006858 [pretransfer_time] => 0.006922 [size_upload] => 0 [size_download] => 27369 [speed_download] => 3465307 [speed_upload] => 0 [download_content_length] => 27369 [upload_content_length] => 0 [starttransfer_time] => 0.007839 [redirect_time] => 0 )
解决方案
There is a CURLOPT_BINARYTRANSFER
option that seems missing. The output you pasted is $info
not $output
btw. It shows the download happened, download_content_length
is 27369.