php - 检测过期 Webhook 的 PHP 函数
问题描述
我一直在使用下面的函数来测试过期的 webhook(超过 10 分钟),它一直工作到今天。
public function isWebhookExpired($eventTime){
$status = TRUE;
$serverTimezone = new DateTimeZone('America/Chicago');
$webhookTimezone = new DateTimeZone('GMT');
$adjustedEventTime = new DateTime($eventTime, $webhookTimezone);
$timeOffset = $serverTimezone->getOffset($adjustedEventTime) - 600;
$currentTime = strtotime("$timeOffset seconds");
if ($currentTime > strtotime($eventTime)) {
error_log('An expired webhook detected.');
}else{
$status = FALSE;
}
return $status;
}
从今天开始,我一直收到错误
PHP Fatal error: Uncaught Exception: DateTime::__construct(): Failed to parse time string (2021-02-01T20:42:09.567506804Z) at position 0 (2): The timezone could not be found in the database
据我所知,时间戳格式没有改变——2021-02-01T20:42:09.567506804Z
我最近确实从 php 7.2 更新到了 7.4,但该功能在更新后一直有效,直到今天。
对于如何解决错误、重新格式化时间戳或完成相同任务的新功能,我将不胜感激。
解决方案
我认为这条线是问题所在
$webhookTimezone = new DateTimeZone('GMT')
该错误告诉您构造函数中不存在时区,并且您在类实例化中使用该 var。
Php 手册说 GMT 已被弃用,所以我只想尝试将 GMT 更改为
欧洲/伦敦或从这里开始的另一个有效时区:
https://www.php.net/manual/en/timezones.europe.php
尝试通过以下方式替换旧的第 5 行:
$webhookTimezone = new DateTimeZone('Europe/London');
推荐阅读
- c++ - 我可以在不同的 linux 上编译相同的 c++ 代码吗?
- python - 如何正确初始化一个类
- java - 通过 jsoup 抓取 Amazon 时数据不按顺序排列
- javascript - 在html视频中制作一个循环按钮
- postgresql - SQL 交叉联接查询未按预期工作
- android-studio - 使用给定位置在 Recicler 视图中更改 Cardview 中的 textview
- gmail - Gmail 身份验证令牌会在一小时内过期
- android - 迁移到世博会后出现白屏
- sql - ORA-00937: 不是单组组函数 00937. 00000 - “不是单组组函数”
- javascript - 在 useEffect 运行后删除事件侦听器导致 scrollIntoView() 永远不会关闭?