32

MY github リポジトリとそのコンテンツだけを外部 Web サイトに表示する場合、どうすればよいでしょうか? 正しい方向に私を向けていない場合、あなたが私に提供できるソースコードはありますか? 私はプログラミングの初心者なので、助けていただければ幸いです。みんなありがとう。彼らのウェブサイトを一瞥する

関連するリンクを一瞥しましたが、どうすればこれを達成できるのかまだわかりません。

-Github のすべてのレポを一覧表示する

-Github すべての Repo コンテンツを一覧表示する

4

8 に答える 8

30

以前の回答はすべて素晴らしいです。ただし、公開されているリポジトリのリストを取得する方法の簡単で汚い例を探している場合は、私の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 Chromes JSONView 拡張機能をインストールしてから、ajax リクエストが作成している URL にアクセスします。つまり、https: //api.github.com/users/google /レポ

于 2013-02-02T00:08:19.070 に答える
11

これは、カールだけの良い方法です。$user 変数と $token 変数を変更して、このスクリプトがケースで機能するようにする必要があります。コードは有効なトークンでテストされているので、うまくいくことを願っています。コードのコメントでわかるように、https://github.com/settings/applicationsの github アカウントからトークンを生成できます。

<?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 が好きなので、結果を最後にキャッシュして、1 日に 1 回程度フェッチする必要があります。

于 2014-04-30T14:59:49.617 に答える
5

これらの例はすべて、「認証」なしの疑似であり、好きなように自分で改善できます。

<?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);
?>
stdClassオブジェクト
((
    [言語]=>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}
    [url] => https://api.github.com/repos/qeremy/mii
    [説明]=>多目的JavaScriptライブラリ
    [ssh_url] => git@github.com:qeremy / mii.git
    [comments_url] => https://api.github.com/repos/qeremy/mii/comments {/ 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);
?>
stdClassオブジェクト
((
    [_links]=>stdClassオブジェクト
        ((
            [自己]=>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') );
?>
于 2013-01-30T12:55:29.413 に答える
3

「リポジトリとその内容を表示する」と言うと、実際には「マスター ブランチの最新のコミット後のリポジトリの状態を表示する」ということですよね? それは実際には問題を考えるより良い方法であり、GitHub の API を使用するためのより良いガイドになります。

API のGit データ部分を確認する必要があります。必要な作業は次のとおりです。

1) 以下を使用して、レポの参照のリストを取得します。

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

作業例:

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

リポジトリ内の参照が一覧表示され、続行するためのリンクが表示されることに注意してください。

2) 1) への応答で提供されたリンクを使用して、関心のある参照、つまり「マスター」のコミット オブジェクトをフェッチします。

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

リポジトリであるルート ディレクトリにあるファイルのリストが返されることに注意してください。これはあなたが望むものです。サブディレクトリがある場合は、それらのサブディレクトリ内のファイルを取得するためのリンクが表示されます。

始めるにはこれで十分です:)。幸運を!

于 2013-01-18T16:47:17.347 に答える
1

git ハブでも利用可能な次のライブラリを試してください: https://github.com/ornicar/php-github-api

于 2013-01-30T13:03:06.103 に答える
0

分析するソース コードが必要な場合は、JavaScript についてGitHub リポジトリ(より具体的には ここ) から開始してみてください。これは、探しているものと同様のことを行う Chrome 拡張機能の優れたオープン プロジェクトです。

于 2013-01-18T02:57:32.667 に答える
0

あなたは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 が得られます。ここから続行できます。

于 2013-10-03T12:13:51.397 に答える
0

Githubs API から返された応答を解析する必要があります。PHP では、これを使用してこれを行うことができますjson_decode()。 which を使用すると、操作する配列が得られます。curlPHP からリクエストを発行し、結果を取得して上記のように解析するようなものを使用できます。
これを行う別の方法は、PHP の REST クライアント クラスです。たとえば、こちらをご覧ください。

于 2013-01-17T23:44:32.867 に答える