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

时间:2014-11-27 18:23:49

标签: php events curl caldav

我想将事件推送到我的webhost上安装的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服务器。知道为什么这不起作用吗?

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

我使用了一种不同的方式来验证和使用版本0.2.7

我切换到了CURLAUTH_DIGEST

补充了PHP返回的基本调整错误。

现在工作正常