使用curl(php)的Google CALDAV XML请求

时间:2014-11-05 19:11:46

标签: php xml curl caldav sabredav

我正在尝试使用PHP对google caldav服务器执行CalDAV XML请求。

由于某些原因,谷歌calDAV记录很差。

目的是获取所有事件的列表,包括特定于事件的数据。 (例如,开始,结束,摘要......)。 目标是尽可能高效地完成此任务。(一个请求中的所有事件数据)。

我发现这可以通过REPORT请求来完成。

我正在使用this post中找到的代码。

我的确切代码:

    $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;
}    

xml请求是从the SabreDAV wiki复制粘贴的。

谷歌在此代码上返回的是“未知错误”。 我知道google的凭据正在运行,因为我使用SabreDAV的内置请求(例如propfind)成功尝试了一些请求。但是SabreDAV无法生成报告请求。

所以我认为xml请求中必定存在google caldav无法正常处理的内容。

我已经花了几天时间摆弄这个,但我似乎找不到合适的解决方案。

1 个答案:

答案 0 :(得分:0)

好吧,您似乎正在使用Google CalDAV API不允许的HTTP Basic Auth:

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

然后,您没有指定哪个网址是您的请求的目标。

相关问题