I'm trying to script a basic CalDAV interaction for use with Apple's iCloud calendars of a given account. At the moment, I'm receiving the response shown below:
Precondition Failed
Requested resource has a matching ETag.
The code I'm using was originally taken from http://trentrichardson.com/2012/06/22/put-caldav-events-to-calendar-in-php/ and adapted to the below:
<?php
$account = array(
'server'=> 'p05',
'id' => '######',
'user' => 'a****[email protected]',
'pass' => '*****'
);
$url = 'https://'.$account['server'].'-caldav.icloud.com/'.$account['id'].'/calendars/work/';
$userpwd = $account['user'] .":". $account['pass'];
$description = 'Test event description';
$summary = 'Test event';
$tstart = gmdate("Ymd\THis\Z", strtotime("-2 days"));
$tend = gmdate("Ymd\THis\Z", strtotime("-2 days"));
$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: *', //Possibly this line causing a problem - unsure of what it does?
'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);
$res = curl_exec($ch);
curl_close($ch);
print_r($res);
?>
You can grab your user ID from this script: https://github.com/muhlba91/icloud/blob/master/PHP/icloud.php
Does anyone have any idea what the response means, or how to solve it? I realise the script is very basic, but I'd like to get something working before tidying it into a class.
Thanks in advance for any advice/help.
Of course after spending hours on a problem and resorting to SO, your brain kicks in.
I was missing the $uid var, needs to be set to a unique (or existing to update) event ID. The below should work for anyone else trying to achieve the same thing:
<?php
$account = array(
'server'=> 'p05',
'id' => '######',
'user' => 'a****[email protected]',
'pass' => '*****'
);
$uid = 'event-12345';
$url = 'https://'.$account['server'].'-caldav.icloud.com/'.$account['id'].'/calendars/work/' . $uid . '.ics';
$userpwd = $account['user'] .":". $account['pass'];
$description = 'Test event description';
$summary = 'Test event';
$tstart = gmdate("Ymd\THis\Z", strtotime("-2 days"));
$tend = gmdate("Ymd\THis\Z", strtotime("-2 days"));
$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);
?>
My mistake.