【问题标题】:How can I download the .zip of my private github repository using cURL on php?如何在 php 上使用 cURL 下载我的私有 github 存储库的 .zip?
【发布时间】:2018-01-30 00:38:07
【问题描述】:

我想下载我正在处理的私有 github 存储库的最新 zip 版本,并且我想使用 PHP 脚本来执行此操作。但是,我当前的 PHP 脚本只是返回“未找到”——我猜我的 cURL 用户/密码设置有问题,但我无法弄清楚。我目前的代码如下:

$username='XXX';
$password='XXX';
$URL='https://github.com/[user]/[reponame]/archive/master.zip';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
$result=curl_exec ($ch);

file_put_contents('master.zip', $result);
curl_close ($ch);

【问题讨论】:

  • 可能与 SSL 绑定?打开跟踪/详细(see this)?具体看VERIFYHOST选项。

标签: php curl github


【解决方案1】:

我能够通过将登录页面和文件下载页面分成两个请求来使其工作。以下代码对我有用:

$username='XXX';
$password='XXX';
$URL='https://github.com/[user]/[reponame]/archive/master.zip';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://github.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");

curl_setopt($ch, CURLOPT_URL,$URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

$result=curl_exec ($ch);
curl_close ($ch);


file_put_contents('master.zip', $result);

【讨论】:

    【解决方案2】:

    除了您的回答,如果您想下载更大的文件以节省内存,我建议您使用以下内容。

    $fp = fopen ('master.zip', 'w+');
    curl_setopt($ch, CURLOPT_FILE, $fp); 
    

    【讨论】:

      【解决方案3】:

      我知道这个问题有点老了,但值得注意的是,GitHub 允许生成 OAuth 密钥,允许您下载私有存储库。这样您就可以不在代码中保留用户名和密码。
      对于未来的寻求者,下面是使用生成的 OAuth 访问令牌的工作示例代码。
      问题在于 zipball 功能,但 API 也允许 tarball。

       $fp = fopen('/path/to/yourfile.zip', 'w+');
       $giturl = 'https://api.github.com/repos/{username}/{reponame}/zipball/master?access_token={YourTokenHere}';
       $ch = curl_init($giturl);
       //set file to write to
       curl_setopt($ch, CURLOPT_FILE, $fp);
       //the API will not allow you to download without a user agent so CURLOPT_USERAGENT is important.
       curl_setopt($ch, CURLOPT_USERAGENT, 'PHP/'.phpversion('tidy'));
       //The API URL redirects so the following line is very important
       curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
       $output = curl_exec($ch);
      
       if(curl_errno($ch)){
           throw new Exception(curl_error($ch));
       }
      
       //Get the HTTP status code.
       $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
      
       //Close the cURL handler.
       curl_close($ch);
      
       //Output result
       if($statusCode == 200){
           echo 'Downloaded Successfully';
       } else{
           echo "Failed downloading - Status Code: " . $statusCode;
       }
      

      【讨论】:

        猜你喜欢
        • 2016-05-30
        • 1970-01-01
        • 2017-02-03
        • 1970-01-01
        • 1970-01-01
        • 2021-10-11
        • 1970-01-01
        • 2018-07-04
        • 2023-01-12
        相关资源
        最近更新 更多