How to Configure Google Cloud API in PHP
Google has many amazing services and today i have chosen Google cloud printing its a really amazing service for offices to manage there printings etc. You have to set a printer with a computer and connect it with Google cloud printer and use this library on you web so any one can easily print from that website by uploading a file.
[wpdm_file id=24]DEMO
index.php it require you Google email address and password to authenticate your account.
require_once 'CloudPrint.php'; $gcp = new GoogleCloudPrint(); if($gcp->loginToGoogle("[email protected]", "PHPGang")) { $printers = $gcp->getPrinters(); $printerid = ""; if(count($printers)==0) { $content = "Could not get printers"; } else { $printerid = $printers[0]['id']; $resarray = $gcp->sendPrintToPrinter($printerid, "Printing Doc using Google Cloud Printing", "upload/" . $_FILES["upload"]["name"], "application/pdf"); if($resarray['status']==true) { $content = "Document has been sent to printer and should print shortly."; } else { $content = "An error occured while printing the doc. Error code:".$resarray['errorcode']." Message:".$resarray['errormessage']; } } }
Call List of Printers
// Make Http call to get printers added by user to Google Cloud Print $responsedata = $this->makeHttpCall(self::PRINTERS_SEARCH_URL,array(),$authheaders); $printers = json_decode($responsedata);
Send file to printer for print
$post_fields = array( 'printerid' => $printerid, 'title' => $printjobtitle, 'contentTransferEncoding' => 'base64', 'content' => base64_encode($contents), // encode file content as base64 'contentType' => $contenttype ); // Prepare authorization headers $authheaders = array( "Authorization: GoogleLogin auth=" . $this->authtoken ); // Make http call for sending print Job $response = json_decode($this->makeHttpCall(self::PRINT_URL,$post_fields,$authheaders)); // Has document been successfully sent? if($response->success=="1") { return array('status' =>true,'errorcode' =>'','errormessage'=>""); }
If you have any problem in using this code please write in comments and need your feedback.
Tutorial Categories:
I tried downloading the files after successfully subscribing to your Feedburner, but it says it can’t find my email.
Dear Will list takes 18 hours to update now list updated you can download file.
I try download and run it. but it is error Login failed please check login info. i ready change my google account.
This isn’t compatible with recent Google changes – the ClientLogin API is deprecated; the example won’t work unless you enable insecure apps in your Google account.
Is it possible to handle multiple printers in same device
Login failed please check login info.
Please help me. THank’s
Login failed please check login info.
Where to download these files.
Please give me a link.
Demo page is not working properly it gives login failed please check login info.
I already subscribed. please give me the code download link