dongtan3395 2012-10-04 12:34
浏览 87
已采纳

用php获取最后一次提交的zip

I found the code below for automated deploying php project. The problem is "https://bitbucket.org/$username/$reponame/get/tip.zip" this url doesnt work on private git repo on bitbucket private git repo: probably related with authentication (I haven't tested this on public repo) What i need is to get the last commit's zip file and unzip inside my project. What am i missing? How to fix this issue

// your Bitbucket username
$username   = "edifreak";

// your Bitbucket repo name
$reponame   = "canvas-game-demo";

// extract to
$dest       = "./"; // leave ./ for relative destination

////////////////////////////////////////////////////////
// Let's get stuff done!

// set higher script timeout (for large repo's or slow servers)
set_time_limit(380);

// download the repo zip file
$repofile = file_get_contents("https://bitbucket.org/$username/$reponame/get/tip.zip");
file_put_contents('tip.zip', $repofile);
unset($repofile);

// unzip
$zip = new ZipArchive;
$res = $zip->open('tip.zip');
if ($res === TRUE) {
    $zip->extractTo('./');
    $zip->close();
} else {
    die('ZIP not supported on this server!');
}

// delete unnecessary .hg files
@unlink("$username-$reponame-tip/.hgignore");
@unlink("$username-$reponame-tip/.hg_archival.txt");

// function to delete all files in a directory recursively
function rmdir_recursively($dir) { 
    if (is_dir($dir)) { 
        $objects = scandir($dir); 
        foreach ($objects as $object) { 
            if ($object != "." && $object != "..") { 
                if (filetype($dir."/".$object) == "dir") rmdir_recursively($dir."/".$object); else unlink($dir."/".$object); 
            } 
        } 
        reset($objects); 
        rmdir($dir); 
    } 
} 

// function to recursively copy the files
function copy_recursively($src, $dest) {
    if (is_dir($src)) {
        if($dest != "./") rmdir_recursively($dest);
        @mkdir($dest);
        $files = scandir($src);
        foreach ($files as $file)
            if ($file != "." && $file != "..") copy_recursively("$src/$file", "$dest/$file"); 
        }
    else if (file_exists($src)) copy($src, $dest);
    rmdir_recursively($src);
}

// start copying the files from extracted repo and delete the old directory recursively
copy_recursively("$username-$reponame-tip", $dest);

// delete the repo zip file
unlink("tip.zip");

// Yep, we're done :)
echo "We're done!";

?>
  • 写回答

1条回答 默认 最新

  • doucuodan0897 2013-06-05 02:59
    关注

    Try putting the authentication (user:pass) in the url:

    $repofile = file_get_contents("https://user:pass@bitbucket.org/$username/$reponame/get/tip.zip");
    

    The authentication must be of a user with read access to the repository.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看