javascriptphpgithub-api

How to get latest commit date - Github API


I'm currently writing a small database of git reps and im wondering how i would go ahead and get the date of the latest commit if i have the rep listed in my database.

I've never worked with the github API and im having a bit of a hard time wrapping my head around it.

If anyone could help me figure it out i'd much appreciate it. PHP or JS prefereably as all the examples ive found has been in ruby.


Solution

  • If you were to use PHP like your example, I'd use cURL instead of file_get_contents, as you'd need to configure allow-url-fopen.

    GitHub also requires you to send a user-agent in the header: https://developer.github.com/v3/#user-agent-required

    For example, your PHP code would look like;

    $objCurl = curl_init();
    
    //The repo we want to get
    curl_setopt($objCurl, CURLOPT_URL, "https://api.github.com/repos/google/blueprint/commits");
    
    //To comply with https://developer.github.com/v3/#user-agent-required
    curl_setopt($objCurl, CURLOPT_USERAGENT, "StackOverflow-29845346"); 
    
    //Skip verification (kinda insecure)
    curl_setopt($objCurl, CURLOPT_SSL_VERIFYPEER, false);
    
    //Get the response
    $response = curl_exec($objCurl);
    print_r( json_decode($response, true) );
    

    Note: You will be able to continue using file_get_contents and send the user-agent header. See this answer