How to Configure Google Cloud API in PHP
Php 05-Nov-2016

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.

index.php it require you Google email address and password to authenticate your account.

require_once 'CloudPrint.php';
        $gcp = new GoogleCloudPrint();
        if($gcp->loginToGoogle("phpgang@gmail.com", "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'=>"");
        }

f you have any problem in using this code please write in comments and need your feedback.