2013-01-19 42 views
4

我正在尝试编写基本的CalDAV交互脚本,以便与给定帐户的Apple iCloud日历一起使用。目前,我收到如下所示的响应:通过PHP的iCloud CalDAV

Precondition Failed 
Requested resource has a matching ETag. 

我正在使用的代码最初是从http://trentrichardson.com/2012/06/22/put-caldav-events-to-calendar-in-php/采取并适用于以下:

<?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); 

?> 

你可以抓住你的用户ID从这个脚本:https://github.com/muhlba91/icloud/blob/master/PHP/icloud.php

有没有人有任何想法的答复意味着什么,或如何解决它?我意识到脚本是非常基本的,但我想在将它整理成一堂课之前先做些工作。

在此先感谢任何建议/帮助。我错过了$ UID VAR

回答

4

当然后一个问题花费时间和诉诸SO,你的大脑踢。

,需要设置一个唯一的(或现有的更新)事件ID。下面应该为其他人试图实现同样的事情:

<?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); 

?> 

我的错误。

+0

你是如何确定你的苹果唯一ID的? 我读过你可以在IOS备份文件中挖掘它,但有没有更简单的方法? – Realistic

+1

嘿,对不起,只是回复。在Apache上本地运行此脚本:https://github.com/muhlba91/icloud/blob/master/PHP/icloud.php – Andy