January 10, 2024 5:01 am

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 = "";
                $content = "Could not get printers";
                $printerid = $printers[0]['id'];
                $resarray = $gcp->sendPrintToPrinter($printerid, "Printing Doc using Google Cloud Printing", "upload/" . $_FILES["upload"]["name"], "application/pdf");
                    $content = "Document has been sent to printer and should print shortly.";
                    $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.

Author Huzoor Bux

I am Huzoor Bux from Karachi (Pakistan). I have been working as a PHP Developer from last 5+ years, and its my passion to learn new things and implement them as a practice. Basically I am a PHP developer but now days exploring more in HTML5, CSS and jQuery libraries.

Tutorial Categories:

8 responses to “How to Configure Google Cloud API in PHP”

  1. Will says:

    I tried downloading the files after successfully subscribing to your Feedburner, but it says it can’t find my email.

  2. Jerry says:

    I try download and run it. but it is error Login failed please check login info. i ready change my google account.

  3. 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.

  4. Sudhi Chowdary says:

    Is it possible to handle multiple printers in same device

  5. bratsadlove says:

    Login failed please check login info.

    Please help me. THank’s

  6. Abhijeet says:

    Login failed please check login info.

  7. Sri Innovate says:

    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

Leave a Reply

Your email address will not be published. Required fields are marked *