问题 使用PHP中的摘要身份验证卷曲请求下载Bitbucket私有存储库


我试着在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] => 
)

有谁知道如何解决我的问题? 非常感谢你!!!


8271
2018-04-25 16:03


起源

您是否尝试过使用cURL的BASIC身份验证(在PHP中)? - ManseUK
嗨,不合作 curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 我没试过 CURLOPT_HTTPAUTH 选项而不是 - Guillermo GF
嗨,我刚尝试了你的例子,它的确有效...所以也许你的帐号,密码或回购都输了? - Zombaya
嗨Zombaya,你搞定了PHP代码吗?用户名和密码都是正确的,因为我把它与php中的命令行相同并且它不起作用,总是重定向到登录页面,我不知道我的错误是什么... - Guillermo GF


答案:


这段代码对我很好:

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');

8
2018-04-17 00:15



没什么,但这对我有用。谢谢。 - Nicholas Pickering
我使用此脚本从下载区域下载某个文件。 - resizemyimg.com


我已经给它了习惯性的“20分钟修补程序”,我会被诅咒,使用CURL和我的OSX HTTP客户端这似乎不想让步,我在过去成功地使用了HTTP Digest和Curl 。

一个建议:如果命令行正常工作,为什么不使用该命令?您的生产服务器是否支持 exec()


1
2018-05-13 18:35



我最后不得不使用 exec() 因为我遇到了同样的问题但是,这非常令人沮丧,特别是如果你没有访问权限的话 exec()。我看了一遍,我找不到可靠的解决方案,或者更重要的是,它找不到工作的原因。 - CWSpear
如果你在这里找到评论: - / - Phil Sturgeon
哈。我有同样的问题,直到我注意到我的用户名变量在结束引号之前包含一个空格,通过exec()调用时工作正常:) - demonkoryu


我有同样的问题;删除所有 密码中的非字母数字字符 它工作了!不知道为什么。

我希望它有所帮助。


1
2017-08-15 11:57