Thursday 29 December 2011

PHP to Paper.......... Where to Start - Authenticating with Google

After being tasked with getting a PHP web application to print for itself, and deciding Google Cloud Print was looking like the best solution, I set about finding the solution to my problem.

After reading the official doc's and deciding either this really isn't how Google currently intended for the service to be used or they really just hadn't had chance to put much into the doc's I widened my search.
I came across a very useful chap called Kin Lane, he has made some excellent blog posts on the subject and turns out he works in the field of printing. After a quick email and him agreeing that I *should* be able to achieve what I wanted with Google Cloud Print, I set about solving my puzzle.

At the time of writing this, there is very little in the way of code examples using Google Cloud Print, in fact the only one I had found focusing on submitting a job was this sample. This however left some questions to which I found no useful answers!

To start with, Google have provided some interfaces for working with the service. Kin Lane has covered connecting to some these services (receiving print jobs, rather than submitting print jobs, but it was a very good jump start) with PHP on his blog. The solution focuses on the use of the Zend Framework, which was handy with it being the framework the web app uses!

The Pseudo Answer:
  • The web app has a dedicated Google user account (in my case, this is one of the Google App accounts which is a group email address) 
  • Setup some printers for the user account as described here.
  • The system requests an Authentication Token from Google to allow use of the service.
  • The system can now access the printers and use the interfaces.
    • The system can now get a list of printers, and
    • The system can now submit jobs to be printed!
The Start of the Real Answer:

After setting up the printers for a user account, as described here.
The first step the system needs to take is getting an Authentication Token from Google which allows us to use the users services, in this case, the printers you have setup.

// Google User Email:
$G_Email = "your_google_acount@googlemail.com";
// Google User Password:
$G_Pass = "your_google_acount_password";
// Create a Gdata client for the Google Cloud Print service:
$client = Zend_Gdata_ClientLogin::getHttpClient($G_Email, $G_Pass, 'cloudprint');
// Get the Authentication Token:
$Client_Login_Token = $client->getClientLoginToken();

Picture taken from Google

You'll notice, the use of the Zend Framework makes getting the Authentication Token extremely easy, however, and this is quite a big however, there is the potential Google will deny an Authentication Token, and instead request a captcha to be completed, this is covered by the Zend Doc's and other guides on the web, but I will try to come back to it later due to this causing an error for an automated system.
Another thing to note is, once Google has served an Authentication Token, it remains valid for several days, I have read this can be anywhere from 14 days to 28 days, so to play it safe, I request a new token every 10 days to play it safe.

In the next entry I will cover the use of the different interfaces to allow us to print.

Saturday 24 December 2011

PHP to Paper..........The Introduction to Printing

Well I'm sure if you have found this blog entry, it's not because you need to be taught how to suck eggs! I recently ran into the brick wall that is, getting a PHP web application to print directly to a printer, no user dialogs, and the printer a sitting on the opposite side of the world to the server, I'm guessing if you have found this, you are looking for a solution to the very same thing.
Google Cloud Print
Imagine this for a situation, you run an online shop, the server is online in a far away place, the office is in one location where workers maintain the shop and your items listed, in another location is your warehouse, every day someone in your warehouse has to log in to print out a picking note and a delivery note.
Now how much easier would it be if every time an order came in, the printer sitting in the warehouse churned out the paperwork the minute your customer confirmed their order?!

Well this was a challenge I had been tasked with, allowing a PHP based back office system to print, no dialogs, no questions asked. So where to start, I wanted a solution which was both simple and easy to maintain. As printers come and go, I wanted a solution that was somewhat future proof and didn't need much code work to change or add printers and the possibility of printing to more than one location.

After a little thinking and a little bit of trawling, I kept coming back to one possibility which the more I thought about the more I thought it's got to hold the answer.
If you haven't heard about it yet, you soon will, the awesome giant that is Google have come up with an excellent new beta product, its called Google Cloud Print, and while at the moment I don't really think it's intended to be an answer to the problem, the more I thought about it, the more I thought it was going to be the answer. Over the next few blog posts I am going to share my findings, and show you how I've used Google Cloud Print to get my PHP system to print directly to my printers, that's the server (in this case) stateside printing directly to a printer roughly 2500 miles away from it!


So a final word to end the introduction, before I get stuck into the nitty gritty: What is Google Cloud Print I hear you ask:

Print anywhere, from any device.
Google Cloud Print is a new technology that connects your printers to the web. Using Google Cloud Print, you can make your home and work printers available to you and anyone you choose, from the applications you use every day. Google Cloud Print works on your phone, tablet, Chromebook, PC, and any other web-connected device you want to print from.
A diagram showing the workflow of Google Cloud Print

Looking for the official help? Click Here for the developers doc's, but be warned as at the time of writing this, the doc's are somewhat limited due to Google Cloud Print still being in beta.
Scroll to Top