2012-04-25 92 views
10

我试图从我的私人到位桶仓库做PHP的这个请求,下载的最后一个来源卷曲要求:在PHP消化AUTH下载到位桶私人仓库

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

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

+0

你尝试使用刚刚从卷曲(内PHP)基本身份验证? – ManseUK 2012-04-25 16:15:34

+1

嗨,不要使用curl_setopt($ ch,CURLOPT_HTTPAUTH,CURLAUTH_BASIC);'我尝试了没有'CURLOPT_HTTPAUTH'选项而不是 – 2012-04-26 07:30:13

+0

嗨,我刚刚试过你的例子,它的工作原理...所以,也许你犯了一个错字你的账户名,密码或回购? – Zombaya 2012-04-28 20:54:50

回答

1

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

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

+0

我刚刚结束了不得不使用'exec()'因为我遇到了同样的问题。但是,这很令人沮丧,特别是如果你没有访问'exec()'。我已经看遍了所有,我找不到一个可靠的解决方案,或者更重要的是,它不工作的原因。 – CWSpear 2012-05-20 02:40:34

+0

如果你在这里发现评论: -/ – 2012-05-20 11:21:04

+0

哈。我有同样的问题,直到我注意到我的用户名变量在结束引号之前包含一个空格,当通过exec()调用时,它工作正常 – demonkoryu 2015-04-16 12:31:04

1

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

我希望它有帮助。

6

此代码工作对我罚款:

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

没有什么,但这对我工作。谢谢。 – 2013-06-13 16:47:56

+0

我用这个脚本从下载区下载某个文件。 – 2013-11-28 13:05:05