nimrod
nimrod

Reputation: 5742

Get events from CalDAV server using curl

I am trying to get all events from a certain CalDAV server (Baikal in this case) with curl. Base tutorial for this task is this.

According to this tutorial, the request should look something like this:

PROPFIND /calendars/johndoe/home/ HTTP/1.1
Depth: 0
Prefer: return-minimal
Content-Type: application/xml; charset=utf-8

<d:propfind xmlns:d="DAV:" xmlns:cs="http://calendarserver.org/ns/">
  <d:prop>
     <d:displayname />
     <cs:getctag />
  </d:prop>
</d:propfind>

This is my implementation:

$headers = array(

        'Content-Type: application/xml; charset=utf-8',
        'Depth: 0',
        'Prefer: return-minimal'
    );

$fp = fopen(dirname(__FILE__).'/getEventCURLError.txt', 'w');
$url = "http://xxx.ch/dav/cal.php/calendars/john/john-cal/";
$userpwd = "john:12345";

// prepare request body
$doc  = new DOMDocument('1.0', 'utf-8');
$doc->formatOutput = true;

$query = $doc->createElement('c:calendar-query');
$query->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:c', 'urn:ietf:params:xml:ns:caldav');
$query->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:d', 'DAV:');

$prop = $doc->createElement('d:prop');
$prop->appendChild($doc->createElement('d:getetag'));
$prop->appendChild($doc->createElement('c:calendar-data'));
$query->appendChild($prop);
$doc->appendChild($query);
$body = $doc->saveXML();

echo "Body: " . $body . "<br>";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, $userpwd);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_STDERR, $fp);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PROPFIND');
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);

$response = curl_exec($ch);
curl_close($ch);

print_r($response);    

$xml = simplexml_load_string($response);
print_r($xml)

The output is this:

Body: 

/dav/cal.php/calendars/john/john-cal/HTTP/1.1 200 OK 

SimpleXMLElement Object ( )

Even though I have a lot of events in the calendar, they don't seem to be returned. What am I doing wrong here? Any input is appreciated.

Upvotes: 0

Views: 6728

Answers (2)

Priyank Kotiyal
Priyank Kotiyal

Reputation: 311

This request is not for getting events from caldav. Its for retrieving calendar information i.e calendarName, calendarId etc. See https://www.rfc-editor.org/rfc/rfc4791#section-7.8.1 for retrieval of events

Method-: REPORT
Content type-: application/xml; charset=utf-8
Header -: Depth : 1
URL-:GET /<ICLOUD_USER_ID>/calendars/
Set credentials
Following is RequestContent-:
<C:calendar-query xmlns:D='DAV:'
                 xmlns:C='urn:ietf:params:xml:ns:caldav'>
                                     <D:prop>
                                       <D:getetag/>
                                       <C:calendar-data>
                                         <C:comp name='VCALENDAR'>
                                           <C:prop name='VERSION'/>
                                           <C:comp name='VEVENT'>
                                             <C:prop name='SUMMARY'/>
                                             <C:prop name='DESCRIPTION'/>
                                             <C:prop name='STATUS'/>
                                              <C:prop name='TRANSP'/>
                                               <C:prop name='ATTENDEE'/>
                                             <C:prop name='UID'/>
                                             <C:prop name='DTSTART'/>
                                             <C:prop name='DTEND'/>
                                             <C:prop name='DURATION'/>
                                             <C:prop name='RRULE'/>
                                             <C:prop name='RDATE'/>
                                             <C:prop name='EXRULE'/>
                                             <C:prop name='EXDATE'/>
                                             <C:prop name='RECURRENCE-ID'/>
                                           </C:comp>
                                         </C:comp>
                                       </C:calendar-data>
                                     </D:prop>
                                     <C:filter>
       <C:comp-filter name='VCALENDAR'>
         <C:comp-filter name='VEVENT'>
           <C:time-range start='20160524T000000Z'
                         end='20160526T000000Z'/>
         </C:comp-filter>
       </C:comp-filter>
     </C:filter>
                                   </C:calendar-query>

Upvotes: 0

hnh
hnh

Reputation: 14815

Presumably your code doesn't work because you are using a calendar-query REPORT entity with a PROPFIND request.

$query = $doc->createElement('c:calendar-query');

...

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PROPFIND');

A PROPFIND request expects a propfind entity in the request, not a calendar-query one, as shown in the original example:

PROPFIND /calendars/johndoe/home/ HTTP/1.1
Depth: 0
Prefer: return-minimal
Content-Type: application/xml; charset=utf-8

<d:propfind xmlns:d="DAV:" xmlns:cs="http://calendarserver.org/ns/">
  <d:prop>
     <d:displayname />
     <cs:getctag />
  </d:prop>
</d:propfind>

You have two choices, either create a proper calendar-query REPORT (i.e. do curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'REPORT')) or emit a proper PROPFIND request entity. I'd say the latter is the better choice.

Upvotes: 2

Related Questions