【问题标题】:iCloud CalDAV via PHP通过 PHP 的 iCloud CalDAV
【发布时间】:2013-01-03 04:49:23
【问题描述】:

我正在尝试编写一个基本的 CalDAV 交互脚本,以便与给定帐户的 Apple iCloud 日历一起使用。目前,我收到如下回复:

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****z@me.com',
    '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

有谁知道响应是什么意思,或者如何解决它?我意识到这个脚本非常基础,但我想在将它整理到一个类中之前让它工作。

提前感谢任何建议/帮助。

【问题讨论】:

    标签: php curl calendar icloud caldav


    【解决方案1】:

    当然,在一个问题上花费数小时并诉诸 SO 之后,你的大脑就会开始工作。

    我缺少 $uid 变量,需要设置为唯一的(或现有的以更新)事件 ID。以下内容应该适用于其他试图实现相同目标的人:

    <?php
    
    $account = array(
        'server'=> 'p05',
        'id'    => '######',
        'user'  => 'a****z@me.com',
        '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);
    
    ?>
    

    我的错。

    【讨论】:

    猜你喜欢
    • 2015-03-06
    • 2015-01-24
    • 2013-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-19
    • 2018-07-07
    • 2013-10-04
    相关资源
    最近更新 更多