通过PHP的iCloud CalDAV
发布时间:2020-12-13 17:15:06 所属栏目:PHP教程 来源:网络整理
导读:我正在尝试编写基本的CalDAV交互脚本,以便与Apple的给定帐户的iCloud日历一起使用.目前,我收到的回复如下: Precondition FailedRequested resource has a matching ETag. 我正在使用的代码最初取自http://trentrichardson.com/2012/06/22/put-caldav-events
我正在尝试编写基本的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("YmdTHisZ",strtotime("-2 days")); $tend = gmdate("YmdTHisZ",strtotime("-2 days")); $tstamp = gmdate("YmdTHisZ"); $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 var,需要设置为唯一(或现有更新)事件ID.以下应该适用于任何试图实现相同目标的人: <?php $account = array( 'server'=> 'p05','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("YmdTHisZ",'Expect: ','Content-Length: '.strlen($body),$body); curl_exec($ch); curl_close($ch); ?> 我的错. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |