如何使用PHP api下载GitHub存储库作为zip档案


How to download GitHub repository as zip archive using PHP api?

我正在尝试使用phpapi将Github存储库下载为zip。有人能帮我解决这个问题吗?

您可以尝试

file_put_contents("master.zip", 
    file_get_contents("https://github.com/{group}/{project}/archive/master.zip")
);

但请确保allow_url_fopen设置在php.ini中,然后您就可以轻松地使用file_get_contents()

您可以将这两块拼图组合起来:

php下载文件:https://stackoverflow.com/a/3938551/2536029

github拉链:https://github.com/$USER/$REPO/archive/$BRANCH.zip例如。https://github.com/bpowers/psm/archive/master.zip

首先,使用以下格式构建一个URL:https://api.github.com/repos/:owner/:repo/:archive_format/:ref

然后,使用curl访问该URL,但不要忘记:

  • 活动跟随位置(CURLOPT_FOLLOWLOCATION),设置为true
  • 分配一个用户代理(CURLOPT_USERAGENT),例如"Mozilla/5.0(Windows NT 5.1;rv:23.0)Gecko/20100101 Firefox/23.0"
  • 如果存储库是私有的,请使用您的用户名和密码(或令牌)进行身份验证,将CURLOPT_USERPWD设置为$youruser.':'$你的通行证
  • 您可以使用CURLOPT_FILE直接加载到文件,只需将该选项设置为使用fopen创建的以前打开的文件指针($filename,'w')

抱歉,没有示例代码,但说明足够清晰。它有效。