Saturday, 7 January 2012

PHP to Paper.......... Getting a list of Printers

If you've read the first part of this little series of posts, you'll know the aim is to allow a PHP web app to print, the web app has just Authenticated with Google and is now ready to start working with the Google Cloud Print Interfaces.

In this post, we'll take the next step to get things up and running, getting a list of the users printers, so we have the needed ID to send print jobs to.
This is really the first stage, really getting stuck into the Google Cloud Print service. When you add a printer to your Google Cloud Print Manager (using the user details which the web app is getting the Authentication Token for) it is given an ID which up till this point is kept hidden but the system will need it.
HINT: To help things out down the line, I would strongly suggest, whenever you add a printer to the Google Cloud Print Manager you rename it to something nice and friendly - you'll see why shortly.
Ok now for a little bit of code. We will start with setting up the client object which will be used for communicating with the interfaces:

// Google User Email:
$G_Email = "";
// 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');

You'll notice, this is the same object we used when we got the authentication token. But we now need to add the following two lines, these need to be present for the Google Cloud Print servers to interact with us.:

// Add the hadders to the Client:
$client->setHeaders('Authorization','GoogleLogin auth='.$Client_Login_Token);

Now that's done, we are ready to start using the interfaces. The next step towards being able to print, is getting a list of the printers which the user account can use. You need this list to get hold of the Google appointed ID for the printer you want to use. To get the list we need to use the search interface. The request will return a JSON object:

// GCP Interface to list printers:
// Request the list of printers:
$response = $client->request(Zend_Http_Client::POST);

We can now perform a quick test on the object to see if there was an authentication error for example:

// Test the response for an auth error:
if ($response->getStatus() == 403) {
    // Handle the error here.........

Now we have the JSON object which contains a couple of details of each of the users printers we can decode it into a PHP Array and we have our list. If however you wanted to allow your user to select a printer to use, you could process the array a little to make it easier to work with, and make it a little more user friendly for your user:

// Decode the result:
$result = json_decode($response->getBody(), true);
// Store an array of printers:
$printerList = array();
// Loop through the printers and to get capabilities:
foreach ($result['printers'] as $printer) {
    // Store details about this printer:
    $currentPrinter = array();
    // Store the printers ID:
    $currentPrinter['id'] =  $printer['id'];
    // Try getting the display name, else get the default name:
    $currentPrinter['name'] = (empty($printer['displayName']))?$printer['name']:$printer['displayName'];
    // Add this printer to the overall list:
    $printerList[] = $currentPrinter;

Now remember above I said rename your printer in the Google Cloud Print Manager, if you did your user will see a user friendly name, if you didn't, they will get the generic printer name, normally its make and model.

So up to now we have:
The next step is......sending though something to print!
Scroll to Top