php - php 和 Microsoft Graph API 中的带时区的 DateTime
问题描述
我正在尝试阅读房间的日历条目并在他们的日历上输出接下来的三个事件。但是,我首先进行测试以查看是否只能获取当天的前 3 个事件,但似乎时区或其他原因导致它无法正确显示事件。
这是我写的一个函数:
function mb_get_meetings($mb_email = null, $mb_proxy = false, $mb_datetime_start = null, $mb_datetime_finish = null)
{
date_default_timezone_set('Australia/Melbourne');
// get the Microsoft Open Graph API
$mb_msgraph = json_decode(mb_microsoft_opengraph($mb_proxy), true); // custom function to get Beaker Token + $mb_proxy for internal proxy on or off for dev testing
$mb_msgraph_token = $mb_msgraph['access_token'];
$mb_datetimenow = new DateTime();
$mb_datetimezone = new DateTimeZone('Australia/Melbourne');
$mb_datetimenow->setTimezone($mb_datetimezone);
$mb_datetime_start = new DateTime($mb_datetime_start, $mb_datetimezone);
$mb_datetime_finish = new DateTime($mb_datetime_finish, $mb_datetimezone);
$mb_datetime_start = $mb_datetime_start->format('Y-m-d\TH:i:s.u');
$mb_datetime_finish = $mb_datetime_finish->format('Y-m-d\TH:i:s.u');
$mb_url_string = 'https://graph.microsoft.com/v1.0/users/' . $mb_email . '/calendar/calendarView?startDateTime=' . $mb_datetime_start . '&endDateTime=' . $mb_datetime_finish;
$mb_args = array(
'headers' => array(
'Authorization' => 'Bearer ' . $mb_msgraph_token,
'Content-Type' => 'application/x-www-form-urlencoded;charset=UTF-8',
'Prefer' => 'outlook.timezone="Australia/Melbourne"'
),
'httpversion' => '1.1'
);
$mb_output = wp_remote_get($mb_url_string, $mb_args);
$mb_output = wp_remote_retrieve_body($mb_output);
return $mb_output;
}
我使用 Wordpress 作为后端,它确实检索了正文。
在我的前端页面中,我调用:
$mbroom = (mb_get_meetings('email@domain.tld', true, 'today 7am', 'today 7pm'));
$mbroom = json_decode($mbroom, true);
$mbroom = $mbroom['value'];
foreach ($mbroom as $k => $v) {
// get the first 3 entries
if ($k < 3) {
print_r($k);
print_r($v['subject']);
print_r(date('g:i', strtotime($v['start']['dateTime']));
print_r(date('g:i', strtotime($v['end']['dateTime']));
print_r($v['organizer']['emailAddress']['name']);
echo '<hr>';
}
}
在结果中,有时我会没有日历条目,而有时我可能会在下午 2 点获得条目,但从早上 8 点开始就没有任何条目。我尝试将其更改为硬编码YYYY-MM-DDD 08:00
,YYYY-MM-DD 20:00
但无济于事。我也尝试过yesterday
,tomorrow
但没有/不正确的结果。
我还尝试预订一整天的 30 分钟会议,但也没有奏效。
难道我做错了什么?
解决方案
我建议以 UTC 格式发送 startDateTime 和 endDateTime 参数,格式为 ISO 8601,如文档中所述(https://docs.microsoft.com/en-us/graph/api/user-list-calendarview?view=graph-rest -1.0&tabs=http ),你已经完成了。但是,我建议使用 PHP 常量,因为它不易出错(https://www.php.net/manual/en/class.datetimeinterface.php#datetime.constants.iso8601)。对这些参数执行以下操作:
$startDateTime = new DateTime(date("Y-m-d H:i:s"));
$startDateTime = $startDateTime->format(DATE_ISO8601);
$startDateTime = substr($startDateTime, 0, strpos($startDateTime, '+'));
编辑:使用子字符串删除 DateTime 字符串的语言环境部分可能是一件幼稚的事情,但它确实起到了作用。
推荐阅读
- javascript - 在使用 javascript 保存之前更改 json 属性 na,e
- ldap - 在公司门户上连接,然后连接到具有单点登录身份验证的聊天应用程序
- python - 解析来自 COVID 暴露网站的 JSON 请求 - 无法打印所需的结果
- mysql - MySQL:DATE_FORMAT 不适用于 %m/%d/%Y 但适用于 %Y/%m/%d
- html - 有没有办法让这个 html 大纲样式不同?
- android - 在 android 8 及更高版本中有什么替代 areNotificationsPaused?
- google-calendar-api - 嵌入时日历不显示忙/闲
- amazon-web-services - 使用 Cloudformation 和 Api Gateway V1 创建 API 映射
- pine-script - 多个警报条件不起作用 - alertcondition(condition=((rsiValue > 70) and (close > upper)) 或 ((rsiValue < 30) and (close < lower))
- dataweave - 在 DataWeave 中的 2 个数据集之间创建一个空行