GCal.php - Google Calendar API in PHP

I’ve been insanely busy the last month or so. I still am this week. But I’m trying to use Google Calendar to help organize my life with Perrie. I created her a calendar, and I linked it with mine. Worked great.

Except, my entries are all off by three hours. All the time zones look fine, but my imported-from-Outlook times are all wrong! Grrr!

So I went to delete the entries from my calendar. Can’t. Well, I can, but not all at once. I wish there was a command line interface to this thing! Then I could just do “rm *”and start over.

And thus was born the gcal.php library. But it’s only just been born. Surely Google will write their own and publish it before I get to a releasable version.

My code is all experimental at this point. Feel free to play with it and send me updates.

I have successfully retrieved my events with wget. And I’ve done some other simple things. But my goal is to get to an API like the GData API for Java/C#.

So far my PHP code can only retrieve the login info from the GData server. Next, I want to:

  • Read existing events
  • Delete existing events
  • Add events
  • Alter events

ps. I hear this SSL code will not work properly on Windows without a DLL update. YMMV.

05/02/06 Update:  I managed to log in, read the full read-write feed, and delete all my events.  yay!  Updated code at the above links.

Next I need to work on a class library to handle this all like the GCal Java library.  Also, Thomas James is working on one, too.  He’s more methodical, but he still managed to beat me in the DELETE race.

8 Responses to “GCal.php - Google Calendar API in PHP”

  1. tim Says:

    You the man. I’ll take a look.

  2. kusamochi Says:

    hey Phord, I took a look at your code, it is brilliant !
    Because I am using windows based php, so I did the dll update as you do.

    there is already a code for php to add event to Google calendar, but it works on *nix systems only because curl won’t allow https on windows build even had the dll updated. so your code is the way to go!!

    I wish you can do some more development on it, as I am currently also working on the same area with another script that combine with yours to make the add event works under windows build php.

  3. kusamochi Says:

    Dear phil,

    I guess u might already done it, but here is a working code extended from yours. I really need to thank you for the brilliant ideas :)

    cheers
    Alan

    “;

    //with help from google forum.

    $host=”66.102.7.99″;
    $method=”post”;
    $path=”/calendar/feeds/default/private/full”;
    $data=” Tennis with Beth Meet for a
    quick lesson. Jo
    Marchj…@gmail.com

    “;

    $method = strtoupper($method);
    $fp = fsockopen(’ssl://’.$host,443);
    fputs($fp,”$method $path HTTP/1.1\r\n”);
    fputs($fp,”Content-Type: application/atom+xml\r\n”);
    fputs($fp,”Authorization: GoogleLogin auth=$auth\r\n”);
    fputs($fp, “Content-Length: ” . strlen($data) . “\r\n”);
    fputs($fp, “Connection: Close\r\n\r\n”);
    fputs($fp, $data);
    while (!feof($fp)) {
    $buf .=fgets($fp);
    }
    fclose($fp);

    //get session_id
    $session_array=explode(”?gsessionid=”,$buf);
    $session_array=explode(”Cache”,$session_array[1]);
    $sessionid=$session_array[0];

    //post xml
    echo $path=”/calendar/feeds/default/private/full”.$sessionid;
    $fp = fsockopen(’ssl://’.$host,443);
    fputs($fp,”$method $path HTTP/1.1\r\n”);
    fputs($fp,”Content-Type: application/atom+xml\r\n”);
    fputs($fp,”Authorization: GoogleLogin auth=$auth\r\n”);
    fputs($fp, “Content-Length: “.strlen($data).”\r\n”);
    fputs($fp, “Connection: Close\r\n\r\n”);
    fputs($fp, $data);
    while (!feof($fp)) {
    $buf .=fgets($fp);
    }
    fclose($fp);
    print $buf;
    ?>

  4. Romain Says:

    Thanks for your ode, it has helped me a lot…

  5. Francis Says:

    For all those of you looking for : How to get the Auth token , Create an event and Get Events using PHP and fsockopen have a look at google PHP
    Cheers,
    Francis

  6. Arroz Says:

    :)

    i just had the same idea! time ago..

    http://arrozconnori.net/gcalendar_gateway/

  7. Vipul Vora Says:

    Hi,

    Is there a working Demo avaiable on any of your servers ?
    I would like to see the demo. I have been searching in google for PHP + Google Calendar Help but I haven;t found any success…

    Regards
    Vipul

  8. George Says:

    Hi,

    May be I’m stupid but tell me how to possible to add event to my calendar?
    With the set of files I’m able>
    OK * Read existing events (working OK)
    OK * Delete existing events (working OK, yeah it deleted all my entry)

    But this is not working…
    ??? * Add events
    ??? * Alter events

    kusamochi.phps as example not working…how to get auth??

    Thanks
    George

Leave a Reply