Curl request with digest auth in PHP for download Bitbucket private repository

后端 未结 3 1371
天涯浪人
天涯浪人 2020-12-31 08:59

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/u         


        
相关标签:
3条回答
  • 2020-12-31 09:23

    This code worked fine for me:

    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');
    
    0 讨论(0)
  • 2020-12-31 09:32

    I had a same problem; removed all the non-alphanumeric characters from the password and it worked! Don't know why though.

    I hope it helps.

    0 讨论(0)
  • 2020-12-31 09:34

    I've given it the customary "20 minute tinker" and I'll be damned, working with CURL and my OSX HTTP Client this does not seem to want to budge, and I've worked successfully with HTTP Digest and Curl in the past.

    One suggestion: if the command line is working then why not use the command? Does your production server support exec()?

    0 讨论(0)
提交回复
热议问题