Hey everyone, I am using the Zend_GData Package to connect to a Google Calendar and do various things such as add/edit/delete events. However, I find that these are pretty expensive operations, and can sometimes take a visible 4 seconds to complete. I was wondering if there is anything I can do to optimize my code and make it faster? Here is an example of my add function:
function add_gcal($title, $date, $where, $desc){
$newIncludePath = array();
$newIncludePath[] = '../ZendGdata-1.8.4PL1/library';
$newIncludePath = implode($newIncludePath);
set_include_path($newIncludePath);
// load classes
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_Calendar');
Zend_Loader::loadClass('Zend_Http_Client');
// connect to service
$user = "******@gmail.com";
$pass = "*******";
$service = Zend_Gdata_Calendar::AUTH_SERVICE_NAME;
$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
try {
$gdataCal = new Zend_Gdata_Calendar($client);
$newEvent = $gdataCal->newEventEntry();
$newEvent->title = $gdataCal->newTitle($title);
$newEvent->where = array($gdataCal->newWhere($where));
$newEvent->content = $gdataCal->newContent("$desc");
$when = $gdataCal->newWhen();
$when->startTime = $date;
$when->endTime = $date;
$newEvent->when = array($when);
$createdEvent = $gdataCal->insertEvent($newEvent);
return $createdEvent->id->text;
} catch (Zend_Gdata_App_Exception $e) {
return NULL;
}
}
Does anyone see if I can make any improvements? For each function (add/delete/edit), I have to do something similar to this, where I load the classes and connect to the service...
Thanks!
EDIT:
I tried setting the $client connection once upon login, and storing $client as a SESSION variable that I could then re-use each time I need to do an operation on the calendar. This doesn't seem to work, I get the following Zend_GData_App_Exception:
exception 'Zend_Gdata_App_HttpException' with message 'Argument is not an instance of Zend_Http_Client.'
in /home/content/b/e/h/behrk2/html/ZendGdata-1.8.4PL1/library/Zend/Gdata/App.php:247
Stack trace:
#0 /home/content/b/e/h/behrk2/html/ZendGdata-1.8.4PL1/library/Zend/Gdata/App.php(172):
Zend_Gdata_App->setHttpClient(Object(__PHP_Incomplete_Class), 'MyCompany-MyApp...')
#1 /home/content/b/e/h/behrk2/html/ZendGdata-1.8.4PL1/library/Zend/Gdata.php(107):
Zend_Gdata_App->__construct(Object(__PHP_Incomplete_Class), 'MyCompany-MyApp...')
#2 /home/content/b/e/h/behrk2/html/ZendGdata-1.8.4PL1/library/Zend/Gdata/Calendar.php(87):
Zend_Gdata->__construct(Object(__PHP_Incomplete_Class), 'MyCompany-MyApp...')
#3 /home/content/b/e/h/behrk2/html/scripts/functions.php(85):
Zend_Gdata_Calendar->__construct(Object(__PHP_Incomplete_Class))
#4 /home/content/b/e/h/behrk2/html/calendar/modify_duty.php(68):
add_gcal(Object(__PHP_Incomplete_Class), 'R4 - Stephen Au...', '2009-08-19', 'Building: Liva ...', 'Primary')
#5 {main}
I also tried using this:
// connect to service
$user = "[email protected]";
$pass = "154Kmb";
$service = Zend_Gdata_Calendar::AUTH_SERVICE_NAME;
$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
$client->setConfig(array('keepalive' => true));
I get no errors here, but it's still slow. I am not doing this upon login and setting a SESSION variable, like I did above, as I got the same error. While this gets no error, it seems to me like it would still make a new connection each time...How would I use this exactly?