booking calDAV event in php



By trent on 22 Jun in PHP, Programming

Toying around with php and pushing events at first seems a bit advanced. However after some digging around it turns out to be quite simple concept. First you must create a string containing an iCalendar format. Then push the contents of that file to the server with fsockopen or curl. Here is a bit of code to push a simple event:

$uid = "test-12345"; // setting this to an existing uid updates event, a new uid adds event $url = $account['uri'].'/'.$uid.'.ics'; //'.$uid.'.ics' $userpwd = $account['user'] .":". $account['pass']; $description = 'My event description here'; $summary = 'My event title 1'; $tstart = '201206015T000000Z'; $tend = '20120616T000000Z'; $tstamp = gmdate("Ymd\THis\Z"); $body = <<<__EOD BEGIN:VCALENDAR VERSION:2.0 BEGIN:VEVENT DTSTAMP:$tstamp DTSTART:$tstart DTEND:$tend UID:$uid DESCRIPTION:$description LOCATION:Office SUMMARY:$summary END:VEVENT END:VCALENDAR __EOD; $headers = array( 'Content-Type: text/calendar; charset=utf-8', 'If-None-Match: *', 'Expect: ', 'Content-Length: '.strlen($body), ); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($ch, CURLOPT_USERPWD, $userpwd); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); curl_setopt($ch, CURLOPT_POSTFIELDS, $body); curl_exec($ch); curl_close($ch);

A long piece of code, but not complex. If we use an existing UID it will be understood as an update. A new UID will create a new event. The hard part will be getting the url at this point. If you are unable to issue a PUT request you may check your Content-Type. I was getting events with empty date fields. I had to specify Content-type: text/calendar. Hope that helps. I have only used this against one server but others are welcome to add to this snippet. Also see this post for more information.

Related posts:

  1. CaldavCFC – Access and Update Caldav with ColdFusion
  2. ColdFusion Style Dumps in PHP
  3. Install PHP Pear Spreadsheet in Ubuntu and Mint
  4. RETS Retrieval with PHP and VieleRETS: Part 1
  5. Make Big Queries More Readable In PHP With Heredoc

One Response

  1. trent - Jul 12, 2012

    I was able to get fsockopen to work. The problem was the Content-Type. My caldav server wants “text/calendar”. I was using “text/icalendar” which was preventing it from working.

2015-06-30 20:25:51gstlouis