使用 PHP 向 Baikal CalDAV 服务器添加事件

Posted

技术标签:

【中文标题】使用 PHP 向 Baikal CalDAV 服务器添加事件【英文标题】:Adding events to Baikal CalDAV server using PHP 【发布时间】:2014-11-27 18:23:48 【问题描述】:

我想将活动推送到安装在我的虚拟主机上的 CalDAV 日历。我正在使用Baikal CalDAV 服务器。

这是我的 php

<?php
    $uid = "test-12345"; // setting this to an existing uid updates event, a new uid adds event
    $url = "http://infobo9.myhostpoint.ch/dav/cal.php/principals/bookify"; //http://mail.domain.com/calendars/DOMAIN/USER/Calendar/'.$uid.'.ics'
    $userpwd = "bookify:test";
    $description = 'My event description here';
    $summary = 'My event title 1';
    $tstart = '20141127T000000Z';
    $tend = '20141127T000000Z';
    $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";

    echo $body . "<br>";

    $headers = array(
        'Content-Type: text/calendar; charset=utf-8',
        'If-None-Match: *',
        'Expect: ',
        'Content-Length: '.strlen($body),
    );
       $fp = fopen(dirname(__FILE__).'/errorlog.txt', 'w');

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    curl_setopt($ch, CURLOPT_STDERR, $fp);
    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);

    echo "done"
?>

PHP 输出:

<<<__EOD BEGIN:VCALENDAR VERSION:2.0 BEGIN:VEVENT DTSTAMP:20141127T181856Z DTSTART:20141127T000000Z DTEND:20141127T000000Z UID:test-12345 DESCRIPTION:My event description here LOCATION:Office SUMMARY:My event title 1 END:VEVENT END:VCALENDAR __EOD
done

我将其作为 CURL 输出:

* About to connect() to infobo9.myhostpoint.ch port 80 (#0)
*   Trying 217.26.52.30... * connected
* Connected to infobo9.myhostpoint.ch (217.26.52.30) port 80 (#0)
* Server auth using Basic with user 'bookify'
> PUT /dav/cal.php/principals/bookify HTTP/1.1
Host: infobo9.myhostpoint.ch
Accept: */*
Content-Type: text/calendar; charset=utf-8
If-None-Match: *
Content-Length: 260

< HTTP/1.1 200 OK
< Date: Thu, 27 Nov 2014 18:02:16 GMT
< Server: Apache/2.2.29 (FreeBSD) DAV/2 mod_ssl/2.2.29 OpenSSL/1.0.1j mod_hcgi/0.9.4
< X-Powered-By: PHP/5.4.32
< Set-Cookie: PHPSESSID=j2q0hmd7tvvo2egbf521tj5q82; path=/; HttpOnly
< Expires: Thu, 19 Nov 1981 08:52:00 GMT
< Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
< Pragma: no-cache
< Transfer-Encoding: chunked
< Content-Type: text/html
< 
* Connection #0 to host infobo9.myhostpoint.ch left intact
* Closing connection #0

事件未添加到 CalDAV 服务器。知道为什么这不起作用吗?

【问题讨论】:

【参考方案1】:

问题在于 URL 不正确。正确的网址是:

http://<domain>/dav/cal.php/calendars/<username>/<calendar-name>/<event-id>.ics

【讨论】:

【参考方案2】:

我使用不同的方式进行身份验证并在 0.2.7 版本中工作

我切换到 CURLAUTH_DIGEST

auguns 基本调整是否由 PHP 返回。

现在可以正常使用了

【讨论】:

以上是关于使用 PHP 向 Baikal CalDAV 服务器添加事件的主要内容,如果未能解决你的问题,请参考以下文章

列出CalDAV(Bedework)上可供用户使用的日历

SabreDAV 作为外部 CALDAV 服务器的客户端库

用于自定义日历的 CalDav 服务器

iOS/OS X 日历应用和 CalDAV 服务自动发现

iCloud日历删除事件请求不适用于caldav

从 iCloud 获取 CalDAV 事件返回“403 Forbidden”