13

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

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!!!

Guillermo GF
  • 133
  • 1
  • 1
  • 5
  • have you tried just using BASIC authentication from cURL (within PHP) ? – Manse Apr 25 '12 at 16:15
  • 1
    Hi, dont work with `curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);` and I tried without `CURLOPT_HTTPAUTH` option and not – Guillermo GF Apr 26 '12 at 07:30
  • Hi, I just tried out your example and it works... So maybe you made a typo for your accountname, password or repo? – Zombaya Apr 28 '12 at 20:54
  • 1
    Hi Zombaya, Do you work out the php code? The username and password are correct, because I put the same that the command line in php and it does not work, always redirected to the login page, I do not know what is my mistake ... – Guillermo GF Apr 30 '12 at 08:06

3 Answers3

12

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');
bruno.braga
  • 1,001
  • 12
  • 21
1

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()?

Phil Sturgeon
  • 30,637
  • 12
  • 78
  • 117
  • I just ended up having to use `exec()` because I was having the same problem. That's pretty frustrating, though, especially if you didn't have access to `exec()`. I've looked all over and I can't find a reliable solution or more importantly, the reason why it's not working. – CWSpear May 20 '12 at 02:40
  • If you find out comment back here :-/ – Phil Sturgeon May 20 '12 at 11:21
  • Hah. I had the same problem, until I noticed that my username variable contained a space before the closing quote, which worked fine when calling via exec() :) – demonkoryu Apr 16 '15 at 12:31
1

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.

Edi Budimilic
  • 4,526
  • 3
  • 19
  • 22