The php method is used here using cUrl (note that I have object level variables called _auth, _username, _password and _printerId).
First create a function to publish with cUrl:
function processRequest($url, $postFields, $referer) { $ret = ""; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_USERAGENT, ""); if(!is_null($postFields)) { curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields); // http_build_query() will properly escape the fields and // build a query string. } if(strlen($this->_auth) > 0) { $headers = array( "Authorization: GoogleLogin auth=". $this->_auth, //"GData-Version: 3.0", "X-CloudPrint-Proxy", "yourappname" ); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); } curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_REFERER, $referer); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $ret = curl_exec ($ch); curl_close ($ch); return $ret; }
Then, the authorization function against Google:
public function authorize() { $url = "https://www.google.com/accounts/ClientLogin"; $post = array("accountType" => "HOSTED_OR_GOOGLE", "Email" => $this->_username, "Passwd" => $this->_password, "service" => "cloudprint", "source" => "yourappname"); $resp = $this->processRequest($url, $post, ""); preg_match("/Auth=([a-z0-9_\-]+)/i", $resp, $matches); $this->_auth = $matches[1]; }
Finally, create a function to send to the cloud printer:
function printDocument($title, $docBytes) { $url = "http://www.google.com/cloudprint/submit?printerid=". $this->_printerId."&output=json"; $post = array( "printerid" => $this->_printerId, "capabilities" => "", "contentType" => "dataUrl", "title" => $title, "content" => 'data:application/pdf;base64,'. base64_encode($docBytes) ); $ret = $this->processRequest($url, $post, ""); echo $ret; }
When using, call authorize () to get the authentication token. Then just read your file (from where) into the variable and pass it to printDocument with a header.