PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Creating an AuthSub authenticated Http Client

Your PHP application should provide a hyperlink to the Google URL that performs authentication. The static function Zend_Gdata_AuthSub::getAuthSubTokenUri() provides the correct URL. The arguments to this function include the URL to your PHP application so that Google can redirect the user's browser back to your application after the user's credentials have been verified.

After Google's authentication server redirects the user's browser back to the current application, a GET request parameter is set, called token. The value of this parameter is a single-use token that can be used for authenticated access. This token can be converted into a multi-use token and stored in your session.

Then use the token value in a call to Zend_Gdata_AuthSub::getHttpClient(). This function returns an instance of Zend_Http_Client, with appropriate headers set so that subsequent requests your application submits using that HTTP Client are also authenticated.

Below is an example of PHP code for a web application to acquire authentication to use the Google Calendar service and create a Zend_Gdata client object using that authenticated HTTP Client.

<?php
$my_calendar 
'http://www.google.com/calendar/feeds/default/private/full';

if (!isset(
$_SESSION['cal_token'])) {
    if (isset(
$_GET['token'])) {
        
// You can convert the single-use token to a session token.
        
$session_token =
            
Zend_Gdata_AuthSub::getAuthSubSessionToken($_GET['token']);
        
// Store the session token in our session.
        
$_SESSION['cal_token'] = $session_token;
    } else {
        
// Display link to generate single-use token
        
$googleUri Zend_Gdata_AuthSub::getAuthSubTokenUri(
            
'http://'$_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'],
            
$my_calendar01);
        echo 
"Click <a href='$googleUri'>here</a> " .
             
"to authorize this application.";
        exit();
    }
}

// Create an authenticated HTTP Client to talk to Google.
$client Zend_Gdata_AuthSub::getHttpClient($_SESSION['cal_token']);

// Create a Gdata object using the authenticated Http Client
$cal = new Zend_Gdata_Calendar($client);

Zend Framework