通过php中的multiget REPORT方法获取日历数据

时间:2014-02-13 23:37:37

标签: php xml calendar caldav

我尝试使用multiget请求在caldav中获取多个事件,如下所示: http://sabre.io/dav/building-a-caldav-client

或在这里: Reading Apple/iCloud calendar data using PHP/CalDAV

假设要像传递一样简单:

<?xml version="1.0" encoding="UTF-8"?>
<B:calendar-multiget xmlns:B="urn:ietf:params:xml:ns:caldav">  
  <A:prop xmlns:A="DAV:">        
  <B:calendar-data/>
  </A:prop>
  <A:href xmlns:A="DAV:">/calendars/__uids__/DE6BFE56-B3DC-4C60-A9C7-ED05A45AAC92/calendar/00ECD3D1-72D0-4F56-9011-71A6180F848C.ics</A:href>
</B:calendar-multiget>

我正在使用PHP传递XML请求。 但XML响应没有任何日历数据:

SimpleXMLElement Object
(
    [response] => SimpleXMLElement Object
        (
            [href] => /calendars/__uids__/DE6BFE56-B3DC-4C60-A9C7-ED05A45AAC92/calendar/00ECD3D1-72D0-4F56-9011-71A6180F848C.ics
            [propstat] => SimpleXMLElement Object
                (
                    [prop] => SimpleXMLElement Object
                        (
                            [calendar-data] => SimpleXMLElement Object
                                (
                                )

                        )

                    [status] => HTTP/1.1 200 OK
                )

        )

)

我使用simplexml_load_string和print_r来显示结果。 如何获取日历数据?

GET方法工作正常,但是当我不想更新&gt; 50个事件时,它太慢了。

修改 根据hnh的建议,我可以使用终端中的Curl。但它在php中使用curl不起作用。这是我使用的代码(url元素,日志,传递,xml请求与终端中使用的相同):

$xml= '<C:calendar-multiget xmlns="DAV:" xmlns:C="urn:ietf:params:xml:ns:caldav"><prop><C:calendar-data/></prop><href>/calendars/__uids__/DE6BFE56-B3DC-4C60-A9C7-ED05A45AAC92/calendar/94DA9025-02E5-4313-BF81-66641BC5989F.ics</href><href>/calendars/__uids__/DE6BFE56-B3DC-4C60-A9C7-ED05A45AAC92/calendar/175CA348-8877-4CDF-B19D-4C66E191F4B4.ics</href></C:calendar-multiget>';

$user = "MyLogin";
$pw= "MyPsw";
$url = "https://localhost:443/calendars/__uids__/DE6BFE56-B3DC-4C60-A9C7-ED05A45AAC92/calendar/";

$data = doRequest($user, $pw, $url, $xml);
print "<pre>";
print_r($data);
print "</pre>";

function doRequest($user, $pw, $url, $xml)
    {
        $c=curl_init($url);
        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);
        curl_setopt($c, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($c, CURLOPT_SSL_VERIFYPEER, 0);
        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;
    }

它不会返回任何日历数据:

/calendars/__uids__/DE6BFE56-B3DC-4C60-A9C7-ED05A45AAC92/calendar/175CA348-8877-4CDF-B19D-4C66E191F4B4.ics

  HTTP/1.1 200 OK

/calendars/__uids__/DE6BFE56-B3DC-4C60-A9C7-ED05A45AAC92/calendar/94DA9025-02E5-4313-BF81-66641BC5989F.ics

  HTTP/1.1 200 OK

2 个答案:

答案 0 :(得分:1)

实际上,Curl请求返回数据。 问题是数据显示。由于日历数据以CDATA形式返回,您必须使用LIBXML_NOCDATA来显示它:

simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA)

答案 1 :(得分:0)

请求有许多多余的命名空间定义,但对我来说看起来还不错。这将是一个较短的版本:

<?xml version="1.0" encoding="UTF-8"?>
<C:calendar-multiget xmlns="DAV:" xmlns:C="urn:ietf:params:xml:ns:caldav">
  <prop><C:calendar-data/></prop>
  <href>/calendars/__uids__/DE6BFE56-B3DC-4C60-A9C7-ED05A45AAC92/calendar/00ECD3D1-72D0-4F56-9011-71A6180F848C.ics</href>
</C:calendar-multiget>

您是否在curl中尝试了请求,以确定服务器返回的内容?像那样:

curl -u "login:pwd" -X REPORT --data \
  '<C:calendar-multiget xmlns="DAV:" xmlns:C="urn:ietf:params:xml:ns:caldav"><prop><C:calendar-data/></prop><href>/calendars/__uids__/DE6BFE56-B3DC-4C60-A9C7-ED05A45AAC92/calendar/00ECD3D1-72D0-4F56-9011-71A6180F848C.ics</href></C:calendar-multiget>' \
  http://yourserver:8008/calendars/__uids__/DE6BFE56-B3DC-4C60-A9C7-ED05A45AAC92/calendar/