Reading free-busy calDAV iCloud PHP

1.5k views Asked by At

I'm trying to read free-busy information from an iCloud calendar.

I based my solution on this php client https://github.com/muhlba91/icloud/blob/master/PHP/icloud.php

What works so far: Getting the Apple user number from an apple ID, using the credentials, this is needed to access the calDAV URL

Getting a list of the available calendars from a user (eg. Home, Work, ...)

What's not working: I cannot get a valid response from the Apple calDAV server.

The request (EDITED):

$ctag_request="<A:propfind xmlns:A='DAV:' xmlns:cs='https://p01-caldav.icloud.com/**USERID**/calendars/home/'>
    <A:prop>
       <cs:getctag />
    </A:prop>
</A:propfind>";


    $response=simplexml_load_string($this->doPropfindRequest($user['appleID'], $user['applepass'], $url, $ctag_request));

This is the DoRequest function. This is exactly the same as the propfind function i'm using to get the userID & list of calendars, except for the CURLOPT_CUSTOMREQUEST which is set to PROPFIND or REPORT. I conclude that the cURL part of my solution is correctly built.

function doReportRequest($user, $pw, $url, $xml)
{
    //Init cURL
    $c=curl_init($url);
    //Set headers
    curl_setopt($c, CURLOPT_HTTPHEADER, array("Depth: 1", "Content-Type: text/xml; charset='UTF-8'", "User-Agent: DAVKit/4.0.1 (730); CalendarStore/4.0.1 (973); iCal/4.0.1 (1374); Mac OS X/10.6.2 (10C540)"));
    curl_setopt($c, CURLOPT_HEADER, 0);
    //Set SSL
    curl_setopt($c, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($c, CURLOPT_SSL_VERIFYPEER, 0);
    //Set HTTP Auth
    curl_setopt($c, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($c, CURLOPT_USERPWD, $user.":".$pw);
    //Set request and XML
    curl_setopt($c, CURLOPT_CUSTOMREQUEST, 'PROPFIND');
    curl_setopt($c, CURLOPT_POSTFIELDS, $xml);
    curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
    //Execute
    $data=curl_exec($c);
    //Close cURL
    curl_close($c);

    return $data;
}

However, what apple returns me on this request is the following:

object(SimpleXMLElement)#259 (1) {
  ["response"]=>
  object(SimpleXMLElement)#261 (2) {
    ["href"]=>
     string(27) "/8126574070/calendars/home/"
     ["propstat"]=>
     object(SimpleXMLElement)#262 (2) {
      ["prop"]=>
      object(SimpleXMLElement)#260 (1) {
        ["getctag"]=>
        object(SimpleXMLElement)#263 (0) {
        }
      }
      ["status"]=>
      string(22) "HTTP/1.1 404 Not Found"
    }
  }
}

I get a 404 response. I think the problem is that the url is wrong. However i can't find anywhere online what the correct apple caldav URL is.

I'm hoping there is some calDAV expert that can help me on this issue.

EDIT

Allright, i edited the code/question since free-busy isn't supported by Apple and it's semi working.

1

There are 1 answers

2
hnh On

The supported-report precondition on the REPORT method is documented over here: http://www.webdav.org/specs/rfc3253.html#rfc.iref.d.17

It simply means that this iCloud calendar does not support freebusy queries. You can see the same in the OS X calendar app if you create an event and open the availability panel. It says 'this calendar does not support availability' ...

To extend the answer to the edited question: This is wrong:

$ctag_request="<A:propfind xmlns:A='DAV:' xmlns:cs='https://p01-caldav.icloud.com/**USERID**/calendars/home/'>
  <A:prop>
    <cs:getctag />
  </A:prop>
</A:propfind>";

The namespace of the getctag property is "http://calendarserver.org/ns/". Try this:

$ctag_request="<propfind xmlns='DAV:'>
  <prop>
    <getctag xmlns='http://calendarserver.org/ns/' />
  </prop>
</propfind>";

The https://p01-caldav.icloud.com/USERID/calendars/home/ is the URL of a calendar collection. It is not an XML namespace.

And yes, if you get a 404, your URL is wrong / doesn't exist.

Another issue might be that your authentication credentials are wrong. Are you providing an application password in your basic auth?