GitHub API - Get Latest Commit Details (PHP)

Want to show off the cool open source project you're working on via GitHub? This PHP code snippet will snag your most recently updated repo information and its latest commit message through the GitHub API. All you have to do is plug in your username.
  <head><title>Test Github API with PHP</title></head>

function get_json($url){
  $base = "";
  $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);
  return $content;

function get_latest_repo($user) {
  // Get the json from github for the repos
    $json = json_decode(get_json("users/$user/repos"),true);

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

$latestRepo = get_latest_repo("joequery");
$commits = get_commits($latestRepo, "joequery");
$latestCommit = $commits[0];

// Relevant information
$repoURL = $latestRepo["html_url"];
$repoName = $latestRepo["name"];
$repoDescription = $latestRepo["description"];
$gravatar = $latestRepo["owner"]["avatar_url"];
$author = $latestCommit["commit"]["author"]["name"];
$login = $latestCommit["author"]["login"];
$userURL = "$login";
$commitMessage = $latestCommit["commit"]["message"];
$commitSHA = $latestCommit["sha"];
$commitURL = "$login/$repoName/commit/$commitSHA";

<h1>Latest Github Activity</h1>
<li>Project: <a href="<?php echo $repoURL; ?>"><?php echo $repoName; ?></a></li>

<li>Description: <?php echo $repoDescription; ?></li>
<li><img src="<?php echo $gravatar; ?>" /></li>
<li>Author: <a href="<?php echo $userURL; ?>"><?php echo $author; ?></a></li>
<li>Last Commit:  <?php echo $commitMessage; ?> (<a href="<?php echo $commitURL; ?>">see commit</a>)</li>


Simply replace my username with yours on lines 41 and 42. You get a lot of information back from the function...just use what you need! (And remember, if you're not sure what information the API/function gives you, print_r is your friend) August 22, 2011
About the Author:

Joseph is the lead developer of Vert Studios Follow Joseph on Twitter: @Joe_Query
Subscribe to the blog: RSS
Visit Joseph's site: