Skip to content

Instantly share code, notes, and snippets.

@xeoncross
Created April 1, 2013 20:03
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save xeoncross/5287320 to your computer and use it in GitHub Desktop.
Save xeoncross/5287320 to your computer and use it in GitHub Desktop.
Fetch a google doc, parse it, cache it, and display the content inside a webpage using PHP.
<?php
// Based on http://www.realisingdesigns.com/2009/10/29/using-google-docs-as-a-quick-and-easy-cms/
function getUrl($url, $expires = 5)
{
$cache_file = __DIR__ . '/cache/' . preg_replace('~\W+~', '-', $url) . '.txt';
if( ! is_dir(__DIR__ . '/cache') AND ! mkdir(__DIR__ . '/cache')) {
die('Please create /cache directory');
}
if (file_exists($cache_file) && (filemtime($cache_file) > (time() - 60 * $expires ))) {
return file_get_contents($cache_file);
}
/*
$options = array(
'http'=>array(
'method'=> "GET",
'header'=>
"Accept-language: en\r\nUser-Agent: Just A Simple Request-er :)\r\n" // i.e. An iPad
/*
//"Cookie: foo=bar\r\n" . // check function.stream-context-create on php.net
"User-Agent: Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.102011-10-16 20:23:10\r\n" // i.e. An iPad
*
)
);
$file = file_get_contents($url, false, stream_context_create($options));
*/
$file = file_get_contents($url);
// Our cache is out-of-date, so load the data from our remote server,
// and also save it over our cache for next time.
$file = file_get_contents($url);
file_put_contents($cache_file, $file, LOCK_EX);
return $file;
}
function getGoogleDoc($id)
{
$content = getUrl("https://docs.google.com/document/pub?id=".$id);
$start = strpos($content,'<div id="contents">');
$end = strpos($content,'<div id="footer">');
$content = substr($content, $start, ($end-$start));
// Fix all embeded image references
$content = str_replace('src="', 'src="https://docs.google.com/document/', $content);
return $content;
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
<title></title>
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<style type="text/css">
body {
/* Gray lines
background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAMAAAAp4XiDAAAAGFBMVEX29vb19fXw8PDy8vL09PTz8/Pv7+/x8fGKuegbAAAAyUlEQVR42pXRQQ7CMBRDwST9pfe/MahEmgURbt7WmpVb6+vG0dd9REnn66xRy/qXiCgmEIIJhGACIZhACCYQgvlDCDFIEAwSBIMEwSBBMEgQDBIEgwTBIEEwCJEMQiSDENFMQmQzCZEbNyGemd6KeGZ6u4hnXe2qbdLHFjhf1XqNLXHev4wdMd9nspiEiWISJgqECQJhgkCYIBAmCIQJAmGCQJggECYJhAkCEUMEwhCBMEQgDJEIQ2RSg0iEIRJhiB/S+rrjqvXQ3paIJUgPBXxiAAAAAElFTkSuQmCC);
*/
/* Straws */
background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAkUlEQVR42pWSQQrAIAwE8/+/evKiB7FsS5fAhkQPqTF0BmW1tdbuve8xBta3Wmsb838W/YMec8MmkaQwVsOnkEQw90aokDiYUgpOJag5J0qucCIRAcp+0+VJ0GP2neBC4mEUYzyRCIyZf0iZJIIlRlQoIaxiSUGuk8DyDiSmCsbeHCyCCvYxxjnXscpDoqSA2T8r6+HomAUULQAAAABJRU5ErkJggg==);
}
#contents {
max-width: 800px;
margin: 0 auto;
}
</style>
</head>
<body lang="en">
<?php
// https://docs.google.com/document/d/______________/pub
$doc_id = "__________";
print getGoogleDoc($doc_id);
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment