2013-01-19 46 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