Adding events to Baikal CalDAV server using PHP

2019-05-14 05:43发布

问题:

I want to push events to a CalDAV calendar that is installed on my webhost. I am using the Baikal CalDAV server.

This is my 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 Output:

<<<__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

I get this as CURL output:

* 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

The event is not being added to the CalDAV server. Any idea why this does not work?

回答1:

The problem was that the URL was not correct. The correct URL is:

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


回答2:

I used a different way to authenticate and worked in the version 0.2.7

I switched to CURLAUTH_DIGEST

Did auguns basic adjustments erroos returned by PHP.

It works fine now