iCloud CalDAV通过PHP [英] iCloud CalDAV via PHP

查看:353
本文介绍了iCloud CalDAV通过PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图编写一个基本的CalDAV交互,用于给定帐户的Apple iCloud日历。目前,我收到以下响应:

 前提条件失败
请求的资源具有匹配的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']。'/日历/工作
$ userpwd = $ account ['user']。:。 $ account ['pass'];
$ 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
位置:Office
摘要:$ summary
END: VEVENT
END:VCALENDAR
__EOD;

$ headers = array(
'Content-Type:text / calendar; charset = utf-8',
'If-None- Match:*',//可能这行引起一个问题 - 不确定是什么?
'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



有没有人知道反应意味着什么,或者如何解决呢?我意识到脚本是非常基本的,但我想在工作之前把它整理成一个类。



先感谢任何建议/帮助。

解决方案

当然, p>

我缺少$ uid var,需要设置为唯一(或现有的更新)事件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 ='测试事件描述';
$ 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
位置:Office
摘要:$ 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);

?>

我的错误。


I'm trying to script a basic CalDAV interaction for use with Apple's iCloud calendars of a given account. At the moment, I'm receiving the response shown below:

Precondition Failed
Requested resource has a matching ETag.

The code I'm using was originally taken from http://trentrichardson.com/2012/06/22/put-caldav-events-to-calendar-in-php/ and adapted to the below:

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

?>

You can grab your user ID from this script: https://github.com/muhlba91/icloud/blob/master/PHP/icloud.php

Does anyone have any idea what the response means, or how to solve it? I realise the script is very basic, but I'd like to get something working before tidying it into a class.

Thanks in advance for any advice/help.

解决方案

Of course after spending hours on a problem and resorting to SO, your brain kicks in.

I was missing the $uid var, needs to be set to a unique (or existing to update) event ID. The below should work for anyone else trying to achieve the same thing:

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

?>

My mistake.

这篇关于iCloud CalDAV通过PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆