如何通过 Github API 获取最新提交日期

8
我正在编写一个小型的git仓库数据库,想知道如果我在我的数据库中列出了该仓库,如何获取最新提交的日期。
我从未使用过GitHub API,有点难以理解。如果有人能帮我弄清楚,我将不胜感激。最好用PHP或JS,因为我找到的所有示例都是用Ruby编写的。

这是你将要使用的API。你能展示一下你尝试过什么,并解释一下哪些没有起作用吗?https://developer.github.com/v3/repos/commits/ - Travis Collins
Github拥有所谓的“RESTful API”,许多在线教程都涉及此类API - 如果您不知道如何轮询此类服务,最好参加一些教授制作此类请求基础的教程。 - nicholaswmin
这是我尝试过的代码:$url = 'https://api.github.com/repos/epenance/hoberthovers'; $obj = json_decode(file_get_contents($url), true);返回的结果是:无法打开流:HTTP 请求失败!HTTP/1.0 403 Forbidden - Martin Hobert
1
您需要执行以下命令:curl -X GET -H "Cache-Control: no-cache" https://api.github.com/repos/<username>/<repo>/commits - ʰᵈˑ
好的,这段代码在我的实际服务器上可以运行,但是在本地主机上却不能。然而,我的数据库中的一些链接会进入它们所在的文件夹,这就是我想要看到更新的链接,比如这个:https://github.com/ikkeflikkeri/LeagueSharp/tree/master/EasyAhri所以我的先前的代码行不起作用。有什么想法吗? - Martin Hobert
3个回答

10
这是一个老问题,但我想指出(至少在api v3中),可以使用分支API获取特定分支的最新提交日期。我猜你在关注master分支。
这将类似于:
https://api.github.com/repos/:owner/:repo/branches/master

请参见https://developer.github.com/v3/repos/branches/

也许显而易见,但是您的存储库必须是公共的,才能通过简单的fetch请求访问github api。 - jimbotron

5
如果你想使用PHP,就像你的例子一样,我会使用cURL代替file_get_contents,因为你需要配置allow-url-fopen
GitHub还要求在头部发送一个user-agenthttps://developer.github.com/v3/#user-agent-required 例如,你的PHP代码将如下所示;
$objCurl = curl_init();

//The repo we want to get
curl_setopt($objCurl, CURLOPT_URL, "https://api.github.com/repos/google/blueprint/commits");

//To comply with https://developer.github.com/v3/#user-agent-required
curl_setopt($objCurl, CURLOPT_USERAGENT, "StackOverflow-29845346"); 

//Skip verification (kinda insecure)
curl_setopt($objCurl, CURLOPT_SSL_VERIFYPEER, false);

//Get the response
$response = curl_exec($objCurl);
print_r( json_decode($response, true) );

注意:您仍然可以继续使用file_get_contents并发送user-agent头。请参见此答案


谢谢,由于JSON不是数组,我该如何在PHP中导航JSON对象? - Martin Hobert
1
json_decode 中删除 , true 以使其成为一个对象。 - ʰᵈˑ
我需要添加 curl_setopt($objCurl, CURLOPT_RETURNTRANSFER, true); 来阻止 curl 自动输出响应。在添加这行代码之前,$response=1 - Mahks
请更新您的代码!!!$sTimeLatest = $aCommit[0]->commit->author->date; $iTimeLatest = strtotime($sTimeLatest);``` 在阅读这些评论之前,我浪费了15分钟! - born loser

4

我想回答这个问题,因此我制作了一个非常小的演示,演示如何获取最新提交的日期。

演示

输出:

ta-dachi
master
2019-03-21T14:50:22Z <----- What you want
b80126c3ea900cd7c92729e652b2e8214ff014d8
https://github.com/ta-dachi/eatsleepcode.tech/tree/master

Github存储库

index.html

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  <title>React Local</title>
  <script
    type="application/javascript"
    src="https://unpkg.com/react@16.0.0/umd/react.production.min.js"
  ></script>
  <script
    type="application/javascript"
    src="https://unpkg.com/react-dom@16.0.0/umd/react-dom.production.min.js"
  ></script>
  <script
    type="application/javascript"
    src="https://unpkg.com/@babel/standalone/babel.min.js"
  ></script>
  <script
    type="application/javascript"
    src="https://unpkg.com/whatwg-fetch@3.0.0/dist/fetch.umd.js"
  ></script>
</head>
<body>
  <div id="root"></div>
  <script type="text/jsx" src="index.jsx"></script>
</body>

index.jsx

/**
 * See https://developer.github.com/v3/repos/branches/#get-branch
 *
 * Example Github api request:
 * https://api.github.com/repos/ta-dachi/eatsleepcode.tech/branches/master
 */
class LatestCommitComponent extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      author: "",
      branch: "",
      date: "",
      sha: "",
      link: ""
    };
  }

  componentDidMount() {
    // Replace this with your own repo
    // https://api.github.com/repos/:owner/:repo/branches/master
    fetch(
      "https://api.github.com/repos/ta-dachi/eatsleepcode.tech/branches/master"
    )
      .then(response => {
        response.json().then(json => {
          console.log(json);
          this.setState({
            author: json.commit.author.login,
            branch: json.name,
            date: json.commit.commit.author.date,
            sha: json.commit.sha,
            link: json._links.html
          });
        });
      })
      .catch(error => {
        console.log(error);
      });
  }

  render() {
    return (
      <div>
        <div>{this.state.author}</div>
        <div>{this.state.branch}</div>
        <div>{this.state.date}</div>
        <div>{this.state.sha}</div>
        <div>{this.state.link}</div>
      </div>
    );
  }
}

ReactDOM.render(<LatestCommitComponent />, document.getElementById("root"));

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接