phpxmlcurlcaldavsabredav

Google CALDAV XML request using curl (php)


I'm trying to do a CalDAV XML request to the google caldav server with PHP.

For some reason the google calDAV is very poorly documented.

The purpose is to get a list of all events, including the event-specific data. (eg. Begin, End, Summary, ...). The goal is to do this as efficiently as possible.(all event data in one request).

I figured out that this can be accomplished by a REPORT request.

I'm using code found in this post.

My exact code :

    $xml= '<c:calendar-query xmlns:d="DAV:" xmlns:c="urn:ietf:params:xml:ns:caldav"><d:prop><c:calendar-data /></d:prop></c:calendar-query>';
    $url = "https://apidata.googleusercontent.com/caldav/v2/*email*/events";
    $user = "**********@gmail.com";
    $pw = "*********";

    $data = $this->doRequest($user, $pw, $url, $xml);
    print_r($data);

}

public function doRequest($user, $pw, $url, $xml)
{
    $c=curl_init();
    $url = preg_replace('{^https?://[^/]+}', '', $url);
    curl_setopt($c, CURLOPT_URL, $url);
    curl_setopt($c, CURLOPT_HTTPHEADER, array("Depth: 1", "Content-Type: text/xml; charset='UTF-8'", "Prefer: return-minimal"));
    curl_setopt($c, CURLOPT_HEADER, 0);
    curl_setopt($c, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($c, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($c, CURLOPT_USERPWD, $user.":".$pw);
    curl_setopt($c, CURLOPT_CUSTOMREQUEST, "REPORT");
    curl_setopt($c, CURLOPT_POSTFIELDS, $xml);
    curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
    $data=curl_exec($c);

    curl_close($c);

    return $data;
}    

The xml request is copy-pasted from the SabreDAV wiki.

What google returns on this code is "Unknown Error". I know the credentials from the google are working, since i successfully tried some request using SabreDAV's built-in requests (eg. propfind). But a report request cannot be generated by SabreDAV.

So i think there must be something in the xml request that google caldav cannot handle properly.

I have been fiddling around with this for several days, but i can't seem to figure out a proper solution.


Solution

  • Well, you seem to be using HTTP Basic Auth which is not allowed by the Google CalDAV API:

    https://developers.google.com/google-apps/calendar/caldav/v2/guide#connecting_to_googles_caldav_server

    Then, you did not specify which URL was the target of your request.