1

我想列出 GitHub 存储库。我可以在浏览器中显示 JSON,但是当我尝试加载 API 页面时,出现 403 错误:

Warning: file_get_contents(https://api.github.com/search/repositories?q=user:<username>): 
failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden`

我的功能如下:

  public function repoListAction() {
    $repositories = json_decode(
      file_get_contents('https://api.github.com/search/repositories?q=user:<put your username here>'),
      true
    );

    return $this->render('full/repolist.html.twig', array(
      'repositories' => $repositories,
    ));
  }
4

1 回答 1

1

GitHub's API documentation lists several reasons for a 403 response code. You've likely reached Github's rate limit for unauthenticated queries. You can confirm this by checking the X-RateLimit-Limit, X-RateLimit-Remaining, and X-RateLimit-Reset headers. You could also be missing a User-Agent header in your request.

You can confirm the exact issue by checking the body of the response, which will contain details of the exact error and reasoning.

于 2016-12-01T10:24:50.100 回答