php源码icloud,通过PHP的iCloud CalDAV

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

Precondition Failed

Requested resource has a matching ETag.

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

?>

有谁知道响应意味着什么,或者如何解决它?我意识到脚本是非常基本的,但我希望在将它整理成一个类之前先得到一些工作.

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

解决方法:

当然,在花费数小时解决问题并诉诸SO之后,你的大脑就开始了.

我错过了$uid var,需要设置为唯一(或现有更新)事件ID.以下应该适用于任何试图实现相同目标的人:

$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,curl,calendar,icloud,caldav

来源: https://codeday.me/bug/20190625/1286352.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值