Google Calendar API PHP Class
Added on: Saturday 22nd August 2009
I recently wanted to update a Google calendar via the API using PHP. There are plenty of samples on the Calendar API site but they all use the Zend framework which I didn't want to use.
I've been working hard on a new version of the projects module of our CRM package which enables users to create quotes that when accepted are converted into tasks that can then be scheduled on a calendar.
This also creates a timesheet that users can then update on a daily basis and any completed tasks are automatically added to a customers account ready for billing.
As part of this I thought it would be a good idea to add any scheduled tasks to a Google Calendar - mainly because its then easy to sync this with Outlook.
As I said I wasn't keen on using the Zend examples as I don't use the framework elsewhere so I searched for a PHP 'wrapper' for the calendar API.
As is often the case, PHP Classes had just what I wanted. The class I found also used another class for the Curl calls. The only problem was that the requirements stated that PHP5 was needed and the server I was using only had 4.4.
However, after a quick look at the two classes it seemed that all I would need to do was change the public and private variable declarations to just var. This did the trick up to a point but although the first curl call to login to the account worked fine, the attempt to add a new event failed.
After a liberal sprinkling of curl_error calls, I tracked this down to the curl_execute function returning the error: Failed to open/read local data from file/application.
A quick search on Google and I came across a post on MilkHub that solved the problem.
The wrapper class calls curl with POST data to login to the account but then uses HTTP headers to update an event. Apparently if the POST data is empty you should explicitly set the CURLOPT_POSTFIELDS to a zero length string, something the curl wrapper class wasn't doing.
A quick change to the curl class and it all worked fine. The files are included here if anyone else is having similar problems.