Я пытаюсь настроить базовое взаимодействие CalDAV для использования с календарями iCloud от Apple в данной учетной записи. На данный момент я получаю ответ, показанный ниже:
Precondition Failed Requested resource has a matching ETag.
Код, который я использую, был первоначально взят из http://trentrichardson.com/2012/06/22/put-caldav-events-to-calendar-in-php/ и адаптирован к приведенному ниже:
<?php $account = array( 'server'=> 'p05', 'id' => '######', 'user' => 'a****z@me.com', 'pass' => '*****' ); $url = 'https://'.$account['server'].'-caldav.icloud.com/'.$account['id'].'/calendars/work/'; $userpwd = $account['user'] .":". $account['pass']; $description = 'Test event description'; $summary = 'Test event'; $tstart = gmdate("Ymd\THis\Z", strtotime("-2 days")); $tend = gmdate("Ymd\THis\Z", strtotime("-2 days")); $tstamp = gmdate("Ymd\THis\Z"); $body = <<<__EOD BEGIN:VCALENDAR VERSION:2.0 BEGIN:VEVENT DTSTAMP:$tstamp DTSTART:$tstart DTEND:$tend UID:$uid DESCRIPTION:$description LOCATION:Office SUMMARY:$summary END:VEVENT END:VCALENDAR __EOD; $headers = array( 'Content-Type: text/calendar; charset=utf-8', 'If-None-Match: *', //Possibly this line causing a problem - unsure of what it does? 'Content-Length: '.strlen($body), ); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($ch, CURLOPT_USERPWD, $userpwd); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); curl_setopt($ch, CURLOPT_POSTFIELDS, $body); $res = curl_exec($ch); curl_close($ch); print_r($res); ?>
, как<?php $account = array( 'server'=> 'p05', 'id' => '######', 'user' => 'a****z@me.com', 'pass' => '*****' ); $url = 'https://'.$account['server'].'-caldav.icloud.com/'.$account['id'].'/calendars/work/'; $userpwd = $account['user'] .":". $account['pass']; $description = 'Test event description'; $summary = 'Test event'; $tstart = gmdate("Ymd\THis\Z", strtotime("-2 days")); $tend = gmdate("Ymd\THis\Z", strtotime("-2 days")); $tstamp = gmdate("Ymd\THis\Z"); $body = <<<__EOD BEGIN:VCALENDAR VERSION:2.0 BEGIN:VEVENT DTSTAMP:$tstamp DTSTART:$tstart DTEND:$tend UID:$uid DESCRIPTION:$description LOCATION:Office SUMMARY:$summary END:VEVENT END:VCALENDAR __EOD; $headers = array( 'Content-Type: text/calendar; charset=utf-8', 'If-None-Match: *', //Possibly this line causing a problem - unsure of what it does? 'Content-Length: '.strlen($body), ); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($ch, CURLOPT_USERPWD, $userpwd); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); curl_setopt($ch, CURLOPT_POSTFIELDS, $body); $res = curl_exec($ch); curl_close($ch); print_r($res); ?>
,<?php $account = array( 'server'=> 'p05', 'id' => '######', 'user' => 'a****z@me.com', 'pass' => '*****' ); $url = 'https://'.$account['server'].'-caldav.icloud.com/'.$account['id'].'/calendars/work/'; $userpwd = $account['user'] .":". $account['pass']; $description = 'Test event description'; $summary = 'Test event'; $tstart = gmdate("Ymd\THis\Z", strtotime("-2 days")); $tend = gmdate("Ymd\THis\Z", strtotime("-2 days")); $tstamp = gmdate("Ymd\THis\Z"); $body = <<<__EOD BEGIN:VCALENDAR VERSION:2.0 BEGIN:VEVENT DTSTAMP:$tstamp DTSTART:$tstart DTEND:$tend UID:$uid DESCRIPTION:$description LOCATION:Office SUMMARY:$summary END:VEVENT END:VCALENDAR __EOD; $headers = array( 'Content-Type: text/calendar; charset=utf-8', 'If-None-Match: *', //Possibly this line causing a problem - unsure of what it does? 'Content-Length: '.strlen($body), ); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($ch, CURLOPT_USERPWD, $userpwd); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); curl_setopt($ch, CURLOPT_POSTFIELDS, $body); $res = curl_exec($ch); curl_close($ch); print_r($res); ?>
Вы можете получить свой идентификатор пользователя из этого скрипта: https://github.com/muhlba91/icloud/blob/master/PHP/icloud.php
Кто-нибудь знает, что означает ответ или как его решить? Я понимаю, что сценарий очень простой, но я хотел бы получить что-то работающее, прежде чем прибирать его в класс.
Заранее благодарим за любой совет / помощь.
Конечно, потратив часы на проблему и прибегая к SO, ваш мозг вздрагивает.
Мне не хватало $ uid var, нужно установить уникальный (или существующий для обновления) идентификатор события. Нижеследующее должно работать для всех, кто пытается добиться того же:
<?php $account = array( 'server'=> 'p05', 'id' => '######', 'user' => 'a****z@me.com', 'pass' => '*****' ); $uid = 'event-12345'; $url = 'https://'.$account['server'].'-caldav.icloud.com/'.$account['id'].'/calendars/work/' . $uid . '.ics'; $userpwd = $account['user'] .":". $account['pass']; $description = 'Test event description'; $summary = 'Test event'; $tstart = gmdate("Ymd\THis\Z", strtotime("-2 days")); $tend = gmdate("Ymd\THis\Z", strtotime("-2 days")); $tstamp = gmdate("Ymd\THis\Z"); $body = <<<__EOD BEGIN:VCALENDAR VERSION:2.0 BEGIN:VEVENT DTSTAMP:$tstamp DTSTART:$tstart DTEND:$tend UID:$uid DESCRIPTION:$description LOCATION:Office SUMMARY:$summary END:VEVENT END:VCALENDAR __EOD; $headers = array( 'Content-Type: text/calendar; charset=utf-8', 'If-None-Match: *', 'Expect: ', 'Content-Length: '.strlen($body), ); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($ch, CURLOPT_USERPWD, $userpwd); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); curl_setopt($ch, CURLOPT_POSTFIELDS, $body); curl_exec($ch); curl_close($ch); ?>
, как<?php $account = array( 'server'=> 'p05', 'id' => '######', 'user' => 'a****z@me.com', 'pass' => '*****' ); $uid = 'event-12345'; $url = 'https://'.$account['server'].'-caldav.icloud.com/'.$account['id'].'/calendars/work/' . $uid . '.ics'; $userpwd = $account['user'] .":". $account['pass']; $description = 'Test event description'; $summary = 'Test event'; $tstart = gmdate("Ymd\THis\Z", strtotime("-2 days")); $tend = gmdate("Ymd\THis\Z", strtotime("-2 days")); $tstamp = gmdate("Ymd\THis\Z"); $body = <<<__EOD BEGIN:VCALENDAR VERSION:2.0 BEGIN:VEVENT DTSTAMP:$tstamp DTSTART:$tstart DTEND:$tend UID:$uid DESCRIPTION:$description LOCATION:Office SUMMARY:$summary END:VEVENT END:VCALENDAR __EOD; $headers = array( 'Content-Type: text/calendar; charset=utf-8', 'If-None-Match: *', 'Expect: ', 'Content-Length: '.strlen($body), ); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($ch, CURLOPT_USERPWD, $userpwd); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); curl_setopt($ch, CURLOPT_POSTFIELDS, $body); curl_exec($ch); curl_close($ch); ?>
,<?php $account = array( 'server'=> 'p05', 'id' => '######', 'user' => 'a****z@me.com', 'pass' => '*****' ); $uid = 'event-12345'; $url = 'https://'.$account['server'].'-caldav.icloud.com/'.$account['id'].'/calendars/work/' . $uid . '.ics'; $userpwd = $account['user'] .":". $account['pass']; $description = 'Test event description'; $summary = 'Test event'; $tstart = gmdate("Ymd\THis\Z", strtotime("-2 days")); $tend = gmdate("Ymd\THis\Z", strtotime("-2 days")); $tstamp = gmdate("Ymd\THis\Z"); $body = <<<__EOD BEGIN:VCALENDAR VERSION:2.0 BEGIN:VEVENT DTSTAMP:$tstamp DTSTART:$tstart DTEND:$tend UID:$uid DESCRIPTION:$description LOCATION:Office SUMMARY:$summary END:VEVENT END:VCALENDAR __EOD; $headers = array( 'Content-Type: text/calendar; charset=utf-8', 'If-None-Match: *', 'Expect: ', 'Content-Length: '.strlen($body), ); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($ch, CURLOPT_USERPWD, $userpwd); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); curl_setopt($ch, CURLOPT_POSTFIELDS, $body); curl_exec($ch); curl_close($ch); ?>
Виноват.