【问题标题】:Github API List all repositories and repo's contentGithub API 列出所有存储库和 repo 的内容
【发布时间】:2013-01-01 15:24:27
【问题描述】:

如果我要在外部网站上仅显示我的 github 存储库及其内容,我将如何执行此操作?如果不能为我指明正确的方向,是否有任何源代码可以提供给我?我是编程的初学者,因此不胜感激。谢谢大家。 浏览他们的网站

我浏览了相关链接,但仍然不知道如何完成此操作。

-Github List all Repo's

-Github List all Repo content

【问题讨论】:

    标签: php javascript github github-api


    【解决方案1】:

    之前的所有答案都很棒。但是,如果您正在寻找一个快速而肮脏的示例来说明如何获取公开可用的存储库列表,请查看我的 jsfiddle.

    它使用这个 ajax 调用来列出所有用户的公共存储库:

    $("#btn_get_repos").click(function() {
        $.ajax({
            type: "GET",
            url: "https://api.github.com/users/google/repos",
            dataType: "json",
            success: function(result) {
                for(var i in result ) {
                    $("#repo_list").append(
                        "<li><a href='" + result[i].html_url + "' target='_blank'>" +
                        result[i].name + "</a></li>"
                    );
                    console.log("i: " + i);
                }
                console.log(result);
                $("#repo_count").append("Total Repos: " + result.length);
            }
        });
    });
    

    要查看返回的数据类型,只需在单击按钮后检查控制台,或者您可以安装 Google Chrome 的 JSONView 扩展程序,然后访问 ajax 请求发出的 url,即https://api.github.com/users/google/repos

    【讨论】:

    【解决方案2】:

    这是一个使用 curl 的好方法。您应该更改 $user 和 $token 变量以使此脚本适用于您的情况。该代码已使用有效令牌进行测试,因此我希望它对您有用。正如您在代码的 cmets 中看到的,令牌可以从您的 github 帐户从这里生成 https://github.com/settings/applications

    <?php
      // for example your user
      $user = 'flesheater';
    
      // A token that you could generate from your own github 
      // go here https://github.com/settings/applications and create a token
      // then replace the next string
      $token = 'ced38b0e522a5c5e8ab10';
    
      // We generate the url for curl
      $curl_url = 'https://api.github.com/users/' . $user . '/repos';
    
      // We generate the header part for the token
      $curl_token_auth = 'Authorization: token ' . $token;
    
      // We make the actuall curl initialization
      $ch = curl_init($curl_url);
    
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    
      // We set the right headers: any user agent type, and then the custom token header part that we generated
      curl_setopt($ch, CURLOPT_HTTPHEADER, array('User-Agent: Awesome-Octocat-App', $curl_token_auth));
    
      // We execute the curl
      $output = curl_exec($ch);
    
      // And we make sure we close the curl       
      curl_close($ch);
    
      // Then we decode the output and we could do whatever we want with it
      $output = json_decode($output);
    
      if (!empty($output)) {
        // now you could just foreach the repos and show them
        foreach ($output as $repo) {
          print '<a href="' . $repo->html_url . '">' . $repo->name . '</a><br />';
        }
      }
    
    ?>
    

    另外,既然我们喜欢github,我们应该把结果缓存到最后,每天取一次左右。

    【讨论】:

      【解决方案3】:

      所有这些例子都是伪的,没有“身份验证”,你可以自己改进它们;

      <?php
      // a simple way to get a user's repo
      $res = file_get_contents("https://api.github.com/repos/qeremy/mii");
      $res = json_decode($res);
      print_r($res);
      ?>
      
      标准类对象 ( [语言] => JavaScript [merges_url] => https://api.github.com/repos/qeremy/mii/merges [contributors_url] => https://api.github.com/repos/qeremy/mii/contributors [assignees_url] => https://api.github.com/repos/qeremy/mii/assignees{/user} [网址] => https://api.github.com/repos/qeremy/mii [描述] => 多用途 JavaScript 库 [ssh_url] => git@github.com:qeremy/mii.git [cmets_url] => https://api.github.com/repos/qeremy/mii/cmets{/number} [statuses_url] => https://api.github.com/repos/qeremy/mii/statuses/{sha} [keys_url] => https://api.github.com/repos/qeremy/mii/keys{/key_id} ...
      <?php
      // getting a repo's README
      $res = file_get_contents("https://api.github.com/repos/qeremy/mii/readme");
      $res = json_decode($res);
      print_r($res);
      ?>
      
      标准类对象 ( [_links] => 标准类对象 ( [自我] => https://api.github.com/repos/qeremy/mii/contents/README.md [git] => https://api.github.com/repos/qeremy/mii/git/blobs/49f0c4d5e25ac44921ba4372aebd76d2da5128e2 [html] => https://github.com/qeremy/mii/blob/master/README.md ) [url] => https://api.github.com/repos/qeremy/mii/contents/README.md [类型] => 文件 [sha] => 49f0c4d5e25ac44921ba4372aebd76d2da5128e2 [路径] => README.md [大小] => 8213 [编码] => base64 [内容] => QWN0dWFsbHksIEkga25vdyB0aGF0IHRoZXJlIGFyZSBidWNoIG9mIEphdmFT Y3JpcHQgbGlicmFyeSwgZXZlbiBtb3JlIHBvd2VyZnVsbC4gQnV0IHNvbWV0 ...

      但是,我认为需要更复杂的结构;

      <?php
      class GRepo
      {
          protected 
              // needs "user"
              $src_userRepos = "https://api.github.com/users/%s/repos",
              // needs "user,repo"
              $src_userRepoDetails = "https://api.github.com/repos/%s/%s",
              $responseCode, $responseText,
              $user;
      
          public function __construct($user) {
              $this->user = $user;
          }
      
          public function listRepos() {
              $this->_request(
                  sprintf($this->src_userRepos, $this->user));
              if ($this->responseCode != 200) {
                  throw new Exception('Server error!'); // e.g
              }
              return json_decode($this->responseText);
          }
      
          public function getRepoDetails($repo) {
              $this->_request(
                  sprintf($this->src_userRepoDetails, $this->user, $repo));
              if ($this->responseCode != 200) {
                  throw new Exception('Server error!'); // e.g
              }
              return json_decode($this->responseText);
          }
      
          // Could be extended, e.g with CURL..
          protected function _request($url) {
              $contents =@ file_get_contents($url);
              $this->responseCode = (false === $contents) ? 400 : 200;
              $this->responseText = $contents;
          }
      }
      
      // Test
      $gr = new GRepo('qeremy');
      print_r( $gr->listRepos() );
      print_r( $gr->getRepoDetails('mii') );
      ?>
      

      【讨论】:

      • 可以说我只是想通过身份验证获得类似 json 的响应(repo 的内容)。我该怎么做?
      【解决方案4】:

      当您说“显示 repo 及其内容”时,您实际上是在说“在 master 分支的最新提交后显示 repo 的状态”,对吗?这实际上是思考问题的更好方式,并且将成为使用 GitHub API 的更好指南。

      您需要查看 API 的 Git data 部分。以下是您需要做的:

      1) 使用以下方法获取您的 repo 的引用列表:

      https://api.github.com/repos/:user/:repo/git/refs
      

      工作示例:

      https://api.github.com/repos/izuzak/noam/git/refs
      

      请注意,它列出了您的 repo 中的引用并为您提供了继续链接。

      2) 使用对 1) 的响应中提供的链接获取您感兴趣的 ref 的提交对象,即“master”:

      https://api.github.com/repos/:user/:repo/git/commits/:sha
      

      工作示例:

      https://api.github.com/repos/izuzak/noam/git/commits/5cf12775b844664d5f7af6663706195680181374
      

      请注意,您会返回一个带有指向树的链接的对象。

      3) 使用对 2) 的响应中提供的链接获取主 ref 中最后一次提交的树对象:

      https://api.github.com/repos/:user/:repo/git/trees/:sha
      

      工作示例:

      https://api.github.com/repos/izuzak/noam/git/trees/8a721bea8d2f281c87b39c74cbf5a70075d686b4
      

      请注意,您会在作为您的存储库的根目录中返回一个文件列表。这就是你想要的。如果您有子目录,您将获得获取这些子目录中文件的链接。

      这应该足以让你开始:)。祝你好运!

      【讨论】:

        【解决方案5】:

        请尝试 git hub 上的以下库: https://github.com/ornicar/php-github-api

        【讨论】:

          【解决方案6】:

          您需要解析 Githubs API 发回的响应。在 PHP 中,您可以使用 json_decode() 来执行此操作,这将为您提供一个可以使用的数组。您可以使用 curl 之类的东西从 PHP 发出请求,然后按上述方式获取结果并解析它们。
          另一种方法是 PHP 的 REST 客户端类,例如,看看这个here

          【讨论】:

            【解决方案7】:

            如果您想分析一些源代码,关于 javascript,您可以尝试从 GitHub Repositories(更具体地说是 here)开始,这是一个很好的 Chrome 扩展程序的开放项目,它与您正在寻找的内容类似为。

            【讨论】:

              【解决方案8】:

              你可以使用github api

              organization="write-here-the-organization"
              githubuser="your-github-user"
              token=`curl -i -u ${githubuser}  -d '{"scopes": ["repo"]}' https://api.github.com/authorizations | grep token | cut -d\" -f 4`
              curl -i -H "Authorization: token ${token}" https://api.github.com/orgs/${organization}/repos 
              

              作为上述结果,您将获得一个包含所有存储库及其信息的长 json。你可以从这里继续。

              【讨论】:

              • 这只会列出存储库中的 30 个项目
              猜你喜欢
              • 1970-01-01
              • 2012-10-04
              • 2015-08-30
              • 1970-01-01
              • 2021-07-11
              • 2022-11-03
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多