[英]Curl request with digest auth in PHP for download Bitbucket private repository
我試着在php上做這個請求,從我的Bitbucket私有存儲庫下載最后一個源:
curl --digest --user user:pass https://bitbucket.org/user/repo/get/tip.zip -o test.zip
在命令行它確定,文件下載完美,但在PHP不工作,這我的PHP代碼:
$out = fopen('test.zip', 'w+');
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
curl_setopt($ch, CURLOPT_USERPWD, 'user:pass');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FILE, $out);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_NOBODY, false);
curl_setopt($ch, CURLOPT_URL, 'https://bitbucket.org/user/repo/get/tip.zip');
curl_exec($ch);
這是響應,登錄無效,服務器重定向到登錄頁面:
Error CURL: ''
Error number: 0
Array
(
[url] => https://bitbucket.org/account/signin/?next=/user/repo/get/tip.tar.gz
[content_type] => text/html; charset=utf-8
[http_code] => 200
[header_size] => 1099
[request_size] => 194
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 1
[total_time] => 1.055465
[namelookup_time] => 1.5E-5
[connect_time] => 0.102969
[pretransfer_time] => 0.216164
[size_upload] => 0
[size_download] => 10049
[speed_download] => 9520
[speed_upload] => 0
[download_content_length] => 10049
[upload_content_length] => 0
[starttransfer_time] => 0.527512
[redirect_time] => 0.527519
[redirect_url] =>
)
誰知道我怎么能解決我的問題? 非常感謝你!!!
這段代碼對我來說很好:
define('USERNAME','username');
define('PASSWORD','password');
function download($url, $destination) {
try {
$fp = fopen($destination, "w");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, USERNAME . ":" . PASSWORD);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$resp = curl_exec($ch);
// validate CURL status
if(curl_errno($ch))
throw new Exception(curl_error($ch), 500);
// validate HTTP status code (user/password credential issues)
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($status_code != 200)
throw new Exception("Response with Status Code [" . $status_code . "].", 500);
}
catch(Exception $ex) {
if ($ch != null) curl_close($ch);
if ($fp != null) fclose($fp);
throw new Exception('Unable to properly download file from url=[' + $url + '] to path [' + $destination + '].', 500, $ex);
}
if ($ch != null) curl_close($ch);
if ($fp != null) fclose($fp);
}
download('https://bitbucket.org/brunobraga/playground/get/tip.tar.gz', '/tmp/test.tar.gz');
我已經給它了習慣性的“20分鍾修補程序”,我會被詛咒,使用CURL和我的OSX HTTP客戶端這似乎不想讓步,我過去曾成功使用HTTP Digest和Curl 。
一個建議:如果命令行正常工作,為什么不使用該命令? 您的生產服務器是否支持exec()
?
我遇到了同樣的問題; 從密碼中刪除所有非字母數字字符 ,它工作正常! 不知道為什么。
我希望它有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.