Skip to content

Instantly share code, notes, and snippets.

@mbmccormick
Created November 7, 2011 16:05
Show Gist options
  • Save mbmccormick/1345367 to your computer and use it in GitHub Desktop.
Save mbmccormick/1345367 to your computer and use it in GitHub Desktop.
Gist caching and download logic.
<?php
// extract gist id from url
$gist = $_GET[id];
// create cache directory
if (file_exists("gists/") == false)
mkdir("gists", 0700);
// check if we have need to update our local copy of the gist
if (file_exists("gists/" . $gist . ".js") == false ||
(time() - filemtime("gists/" . $gist . ".js")) > 1209600 ||
$_GET[force] == "true") // if file exists, or file is greater than 14 days old, or force update
{
// download javascript file from github
$ch = curl_init("https://gist.github.com/" . $gist . ".js");
$fp = fopen("gists/" . $gist . ".js", "wb");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
}
// open local copy of gist
$fp = fopen("gists/" . $gist . ".js", "r");
// parse javascript
// render gist content
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment