Skip to content

Instantly share code, notes, and snippets.

@tresf
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tresf/df203d1952014aef930c to your computer and use it in GitHub Desktop.
Save tresf/df203d1952014aef930c to your computer and use it in GitHub Desktop.
<?php
// JSON releases URL
$json_releases = 'https://api.github.com/repos/LMMS/lmms/releases';
// JSON releeases cache file
$cache_file = $_SERVER['DOCUMENT_ROOT_HASH'] . '/../tmp/.json_github_releases';
// Use the cache version if it's less than 60 seconds old
if (file_exists($cache_file) && (filemtime($cache_file) > (time() - 60))) {
$json = file_get_contents($cache_file);
} else {
$json = file_get_contents_curl($json_releases);
file_put_contents($cache_file, $json, LOCK_EX);
}
// decode json data
$obj = json_decode($json);
// loop through items and echo
foreach($obj as $item) {
echo '<h3>LMMS ' . $item->tag_name . '</h3><ul>';
foreach($item->assets as $asset) {
echo '<li><a href="' . $asset->browser_download_url . '">' .
$asset->name . '</a>:&nbsp;';
echo '(' . $asset->download_count . ')</li>';
}
echo '</ul>';
}
// file_get_contents() won't work. Use curl instead.
function file_get_contents_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, 'curl/7.x (linux)');
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment