Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
I got sick of having to maintain my resume in multiple formats in multiple areas online, so I whipped this together to pull from my Google Docs acct. and save local copies so I can do various things with them (embed html, link to a pdf, etc.)
<?php
/**
* Warning: I'm using the v3.0 Protocol here, which hasn't "graduated" yet.
**/
/** basic setup **/
$ch=curl_init();
$h_auth=array();
$h_docs=array();
$save_dir = '/dir/to/save/your/file';
$h_auth['accountType']='GOOGLE';
$h_auth['Email']='your-email-address@gmail.com';
$h_auth['Passwd']='your-google-password';
$h_auth['source']='sitename-appname-version';
$h_auth['service']='writely'; // the google docs service
/** get the auth token **/
curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/accounts/ClientLogin' );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0 );
curl_setopt($ch, CURLOPT_POST, true );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_POSTFIELDS, $h_auth );
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
try {
$result = curl_exec($ch);
} catch( Exception $e ) {
echo "Curl error:" . curl_error($ch);
die();
}
if(strpos($result,'Error') !== false ) {
$data = explode('=',$result);
trigger_error( "Google responded - " . $data[1] );
curl_close($ch);
exit(0);
} elseif( ( $pos = strpos($result, 'Auth=') ) !== false ) {
$token = trim(substr($result, $pos+5,strlen($result)-1));
} else {
trigger_error( "Unknown response from google", E_USER_NOTICE );
curl_close($ch);
exit(0);
}
/** OK so far - get the info about the doc **/
$h_docs= array(
"Authorization: GoogleLogin auth=" . $token,
"GData-Version: 3.0",
);
curl_setopt($ch, CURLOPT_URL, "https://docs.google.com/feeds/default/private/full?title=your-resume-name" );
curl_setopt($ch, CURLOPT_HTTPHEADER, $h_docs );
curl_setopt($ch, CURLOPT_POST, false );
$xml = curl_exec($ch);
$xmlObj=simplexml_load_string($xml);
if($xmlObj->HEAD->TITLE == 'Token invalid' ) {
trigger_error( "Google responded - Invalid Token", E_USER_NOTICE );
exit(0);
}
$base_url = $xmlObj->entry->content->attributes()->src;
/** possible types: doc, html, jpeg, odt, pdf, png, rtf, svg, txt, zip **/
$my_resume = "myresume-name";
/** save a text copy **/
curl_setopt($ch, CURLOPT_URL, $base_url ."&exportFormat=txt");
$fp=fopen("$save_dir/$my_resume.txt", 'w' );
curl_setopt($ch, CURLOPT_FILE, $fp );
curl_exec($ch);
fclose($fp);
/** save a pdf **/
curl_setopt($ch, CURLOPT_URL, $base_url . "&exportFormat=pdf");
$fp=fopen("$save_dir/$my_resume.pdf",'w' );
curl_setopt($ch, CURLOPT_FILE, $fp );
curl_exec($ch);
fclose($fp);
/** save html **/
curl_setopt($ch, CURLOPT_URL, $base_url . "&exportFormat=html");
$fp=fopen("$save_dir/$my_resume.html",'w');
curl_setopt($ch, CURLOPT_FILE, $fp );
curl_exec($ch);
fclose($fp);
/** save doc **/
curl_setopt($ch, CURLOPT_URL, $base_url . '&exportFormat=doc' );
$fp = fopen("$save_dir/stefan-j-antonowicz.doc", 'w' );
curl_setopt($ch, CURLOPT_FILE, $fp );
curl_exec($ch);
fclose($fp);
curl_close($ch);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment