【问题标题】:GitHub API commitsGitHub API 提交
【发布时间】:2017-05-09 15:48:28
【问题描述】:

这里有人知道如何使用 GitHub API 吗? 我正在尝试创建一个表来检索所有存储库提交 (https://api.github.com/repos/RubeVi/Fenix/commits)。只想显示sha,但看起来我的代码是错误的

<?php
$json = file_get_contents("https://api.github.com/repos/RubeVi/Fenix/commits");
$obj = json_decode($json, true);
?>
<table>
<?php foreach ($obj as $o) { ?>
    <tr>
        <td><?php echo $o["sha"]; ?></td>
        <td><?php echo $o["commit"]["author"]["name"]; ?></td>
        <td><?php echo $o["commit"]["author"]["email"]; ?></td>
        <td><?php echo $o["commit"]["message"]; ?></td>
    </tr>
<?php } ?>
</table>

【问题讨论】:

  • 正确标记事物。
  • ...因为您的代码中有错误的 URL?
  • @duskwuff 发生了很多事情,伙计......
  • @duskwuff 很复杂
  • 没有回答,需要$opts = ['http' =&gt; ['method' =&gt; 'GET', 'header' =&gt; ['User-Agent: PHP']]]; $context = stream_context_create($opts);

标签: php json github-api


【解决方案1】:

这是你所期待的吗?

如果是这样,这样做也更容易看到:

<?php
$opts = ['http' => ['method' => 'GET', 'header' => ['User-Agent: PHP']]];
$context = stream_context_create($opts);
$json = file_get_contents("https://api.github.com/repos/RubeVi/Fenix/commits", false, $context);
$obj = json_decode($json, true);
?>
<table>
<?php foreach ($obj as $o) { ?>
    <tr>
        <td><?php echo $o["sha"]; ?></td>
        <td><?php echo $o["commit"]["author"]["name"]; ?></td>
        <td><?php echo $o["commit"]["author"]["email"]; ?></td>
        <td><?php echo $o["commit"]["message"]; ?></td>
    </tr>
<?php } ?>
</table>

使用file_get_contents() gets 403 from api.github.com everytime修复了问题

【讨论】:

    猜你喜欢
    • 2010-12-13
    • 1970-01-01
    • 2014-03-19
    • 2020-12-31
    • 1970-01-01
    • 2011-06-14
    • 2021-02-11
    • 1970-01-01
    相关资源
    最近更新 更多