Using the GitHub API and PHP to Get Repository Information

By  on  

GitHub is an awesome place to host your open source project code. MooTools, Prototype, and jQuery all use GitHub. As you probably know, the MooTools Forge requires your plugins be hosted on GitHub. The only problem with hosting all my MooTools plugins is that I lose traffic when I want people to see my code. Problem solved: use PHP, the GitHub API, and PHP Markdown to display files of my choice on my website.

Our goals with this code will be to:

  • Connect to GitHub via the API to retrieve repository information.
  • Retrieve the content of two files from the repository: a source file and the Markdown file.
  • Cache the information for a given period of time to reduce the load on GitHub.
  • Use PHP Markdown to output a formatted file.

I know that seems like a lot of work but you'll be amazed at how easy the process is.

PHP MarkDown

You may download PHP Markdown at Michel Fortin's website. It's simple and full of features.


The first step is to build a PHP function that will connect to GitHub using cURL:

/* gets url */
function get_content_from_github($url) {
	$ch = curl_init();
	$content = curl_exec($ch);
	return $content;

Next we need to define a few settings:

/* static settings */
$plugin = 'Overlay';
$cache_path = $_SERVER['DOCUMENT_ROOT'].'/plugin-cache/';
$cache_file = $plugin.'-github.txt';
$github_json = get_repo_json($cache_path.$cache_file,$plugin);

The next step is to create another PHP function that grabs the repository information (JSON-encoded, because I love JSON) -- either fresh from GitHub (by first grabbing the most recent commit hash, then grabbing the contents of the two files) or our local cached information:

/* gets the contents of a file if it exists, otherwise grabs and caches */
function get_repo_json($file,$plugin) {
	$current_time = time(); $expire_time = 24 * 60 * 60; $file_time = filemtime($file);
	//decisions, decisions
	if(file_exists($file) && ($current_time - $expire_time < $file_time)) {
		//echo 'returning from cached file';
		return json_decode(file_get_contents($file));
	else {
		$json = array();
		$json['repo'] = json_decode(get_content_from_github(''.$plugin),true);
		$json['commit'] = json_decode(get_content_from_github(''.$plugin.'/master'),true);
		$json['readme'] = json_decode(get_content_from_github(''.$plugin.'/'.$json['commit']['commits'][0]['parents'][0]['id'].'/Docs/'.$plugin.'.md'),true);
		$json['js'] = json_decode(get_content_from_github(''.$plugin.'/'.$json['commit']['commits'][0]['parents'][0]['id'].'/Source/'.$plugin.'.js'),true);
		return $content;

Once we've acquired the appropriate information, we output the information to screen:

/* build json */
if($github_json) {
	//get markdown
	//build content
	$content = '<p>'.$github_json['repo']['repository']['description'].'</p>';
	$content.= '<h2>MooTools JavaScript Class</h2><pre class="js">'.$github_json['js']['blob']['data'].'</pre><br />';
	$content.= trim(str_replace(array('<code>','</code>'),'',Markdown($github_json['readme']['blob']['data'])));

That's all! Now I get the benefit of hosting my code on GitHub but displaying it on my own website. I've created a special WordPress template page to do so and recommend you do too!


Visit my Projects page and click on the "Docs" link for any project. All of the information that comes up on individual project pages comes from GitHub. No more manual page creation!

Recent Features

Incredible Demos

  • By
    Facebook Open Graph META Tags

    It's no secret that Facebook has become a major traffic driver for all types of websites.  Nowadays even large corporations steer consumers toward their Facebook pages instead of the corporate websites directly.  And of course there are Facebook "Like" and "Recommend" widgets on every website.  One...

  • By
    Better Pull Quotes with MooTools

    Chris Coyier authored a post titled Better Pull Quotes: Don't Repeat Markup a while back. In his post he created great-looking pull quotes without repeating any content -- instead he uses jQuery to dynamically create the pull quotes. The following is the...


  1. Roy Sutton

    Very nice, exactly what I was looking for. I did notice that the last line of get_repo_json() should be return $json instead of return $content.

  2. Hi, nice article.
    I would love to try it, but your demo seems to fail.

  3. David

    Is there a way to get the contents of a private repository through php if you have a user name and password and permissions on the repository? Any direction how to go about it?

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!