php - iCloud CalDAV 通过 PHP

标签 php curl calendar icloud caldav

我正在尝试编写一个基本的 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

有人知道响应的含义或解决方法吗?我意识到该脚本非常基础,但我希望在将其整理到类中之前让一些东西正常工作。

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

最佳答案

当然,在花费数小时解决问题并求助于 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);

?>

我的错误。

关于php - iCloud CalDAV 通过 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14416501/

相关文章:

php - 使用 PHP 解码 Microsoft 翻译器 API 的 JSON 输出

c# - asp.net 日历控件触发验证控件

php - 本地化的州/省数据

php - 从 mysql 中的 3 列中选择文件并压缩它们

bash - Ubuntu - curl X 次重复

linux - Curl 无法在实时服务器上使用 http/socks 代理

python - 如何在python中确定一周的第一天

java - 基于 Java/Groovy 时区的日期时间转换

javascript - 循环隐藏和显示表格行

javascript - 将验证添加到动态创建的文本框 JavaScript PHP