curl请求与摘要auth在PHP中下载Bitbucket私人存储库 [英] Curl request with digest auth in PHP for download Bitbucket private repository

查看:147
本文介绍了curl请求与摘要auth在PHP中下载Bitbucket私人存储库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在php上执行此请求,从我的Bitbucket私人存储库下载最后一个源代码:

I'm try to do this request on php, for download the last source from my Bitbucket private repository:

curl --digest --user user:pass https://bitbucket.org/user/repo/get/tip.zip -o test.zip

在命令行中它的ok,文件下载完美,但在php dont工作,这我的PHP代码:

in command line its ok, the file download perfect, but in php dont work, this my php code:

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

这是响应,登录无效,服务器重定向到登录页面:

This is the response, the login its invalid and the server redirect to the login page:

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] => 
)

任何人都知道我如何解决我的问题?
非常感谢!

Anyone know how I can solve my problem? Thank you very much!!!

推荐答案

这段代码对我很好:

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请求与摘要auth在PHP中下载Bitbucket私人存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆