在PHP中使用摘要身份验证的Curl请求,用于下载Bitbucket私有存储库
我尝试在php上执行此请求,以便从我的Bitbucket私有存储库下载最后一个源代码:在PHP中使用摘要身份验证的Curl请求,用于下载Bitbucket私有存储库,php,curl,command-line,bitbucket,digest-authentication,Php,Curl,Command Line,Bitbucket,Digest Authentication,我尝试在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_RETU
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] =>
)
有人知道我如何解决我的问题吗?
非常感谢 我给了它惯用的“20分钟修补程序”,我会被诅咒的,使用CURL和我的OSX HTTP客户端这似乎不想改变,我在过去成功地使用了HTTP摘要和CURL
一个建议:如果命令行正在工作,那么为什么不使用该命令呢?您的生产服务器是否支持exec()?我也遇到了同样的问题;从密码中删除所有非字母数字字符,它成功了!但我不知道为什么
我希望它能有所帮助。这段代码对我来说运行良好:
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');
您是否尝试过使用cURL的基本身份验证(在PHP中)?嗨,不要使用
cURL\u setopt($ch,CURLOPT\u HTTPAUTH,CURLAUTH\u BASIC)代码>我尝试了不使用CURLOPT_HTTPAUTH
选项的方法,但是没有。我只是尝试了一下你的例子,效果很好。。。所以,也许你输入了你的帐号名、密码或repo?嗨,Zombaya,你能写出php代码吗?用户名和密码是正确的,因为我在php中使用了相同的命令行,但它不起作用,总是重定向到登录页面,我不知道我的错误是什么…我只是因为遇到了相同的问题而不得不使用exec()
。但是,这非常令人沮丧,尤其是如果您没有访问exec()
的权限。我找遍了所有地方,找不到可靠的解决方案,更重要的是,找不到它不起作用的原因。如果你在这里找到评论:-/Hah。我也有同样的问题,直到我注意到我的username变量在结束引号之前包含了一个空格,这在通过exec()调用时运行良好:)我使用这个脚本从下载区域下载某个文件。