john
john

Reputation: 1

Github api request with php curl

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);
}

Upvotes: 0

Views: 2241

Answers (1)

Iguannaweb
Iguannaweb

Reputation: 11

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');

Upvotes: 1

Related Questions