0

I am trying to get the latest commit from github using the api, but I encounter some errors and not sure what the problem is with the curl requests. The CURLINFO_HTTP_CODE gives me 000.

What does it mean if I got 000 and why is it not getting the contents of the 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);
}
3
  • What's your question? Commented Mar 1, 2016 at 17:58
  • So the URL = https://api.github.com + users/$user/repos = https://api.github.comusers/$user/repos should be obvious what the issue is now. Commented Mar 1, 2016 at 18:10
  • I fixed that But got an 403 error Commented Mar 1, 2016 at 18:25

1 Answer 1

1

I use your code and it will work if you add an user agent on curl

curl_setopt($ch, CURLOPT_USERAGENT,'YOUR_INVENTED_APP_NAME');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.