首页 > 解决方案 > 检测过期 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,但该功能在更新后一直有效,直到今天。

对于如何解决错误、重新格式化时间戳或完成相同任务的新功能,我将不胜感激。

标签: phpdatetimetimestampwebhooks

解决方案


我认为这条线是问题所在

  $webhookTimezone = new DateTimeZone('GMT')

该错误告诉您构造函数中不存在时区,并且您在类实例化中使用该 var。

Php 手册说 GMT 已被弃用,所以我只想尝试将 GMT 更改为

欧洲/伦敦或从这里开始的另一个有效时区:

https://www.php.net/manual/en/timezones.europe.php

尝试通过以下方式替换旧的第 5 行:

$webhookTimezone = new DateTimeZone('Europe/London');

推荐阅读