【问题标题】:Github api request with php curl带有 php curl 的 Github api 请求
【发布时间】:2016-06-14 06:36:24
【问题描述】:

我正在尝试使用 api 从 github 获取最新的提交,但是我遇到了一些错误并且不确定 curl 请求的问题是什么。 CURLINFO_HTTP_CODE 给了我 000。

如果我得到 000 是什么意思,为什么它没有获取 url 的内容?

function get_json($url){
  $base = "https://api.github.com";
  $curl = curl_init();
  curl_setopt($curl, CURLOPT_URL, $base . $url);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

    //curl_setopt($curl, CONNECTTIMEOUT, 1);
  $content = curl_exec($curl);
  echo $http_status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  curl_close($curl);

  return $content;
}
echo get_json("users/$user/repos");
function get_latest_repo($user) {
  // Get the json from github for the repos
    $json = json_decode(get_json("users/$user/repos"),true);
    print_r($json);
  // Sort the array returend by pushed_at time
  function compare_pushed_at($b, $a){
    return strnatcmp($a['pushed_at'], $b['pushed_at']);
  }
  usort($json, 'compare_pushed_at');

  //Now just get the latest repo
  $json = $json[0];

  return $json;
}

function get_commits($repo, $user){
  // Get the name of the repo that we'll use in the request url
  $repoName = $repo["name"];  
  return json_decode(get_json("repos/$user/$repoName/commits"),true);
}

【问题讨论】:

  • 你有什么问题?
  • 所以 URL = https://api.github.com + users/$user/repos = https://api.github.comusers/$user/repos 应该很明显现在的问题是什么。
  • 我修复了,但出现 403 错误

标签: php curl github


【解决方案1】:

我使用您的代码,如果您在 curl 上添加用户代理,它将起作用

curl_setopt($ch, CURLOPT_USERAGENT,'YOUR_INVENTED_APP_NAME');

【讨论】:

    猜你喜欢
    • 2021-02-10
    • 1970-01-01
    • 2022-01-28
    • 1970-01-01
    • 1970-01-01
    • 2011-10-30
    • 1970-01-01
    • 1970-01-01
    • 2021-02-21
    相关资源
    最近更新 更多