服务器时间不对导致短信无法发送

tinker:

$sms = app('easysms');
try {
    $sms->send(13212345678, [
         'template' => $template,
         'data' => [
             'code' => 1234
         ],
    ]);
} catch (\Overtrue\EasySms\Exceptions\NoGatewayAvailableException $exception) {
    $message = $exception->getException('aliyun')->getMessage();
    dd($message);
}

tinker返回信息:

Client error: `GET http://dysmsapi.aliyuncs.com?......` resulted in a `400 Bad Request` response:\n
{"RequestId":".....","Message":"Specified time stamp or date value is expired.","Recommen (truncated...)\n
"""

注意上面的 Message:
Specified time stamp or date value is expired.
意思大概是时间戳或日期值过期了。

解决方案

使用ntpdate同步时间:

$ sudo ntpdate time.windows.com

番外

如果去掉try、catch

$sms = app('easysms');
$sms->send($phone, [
  'template' => $template,
     'data' => [
  'code' => 1234
     ]
 ]);

仍然无法发送短信,
而且tinker只会报

All the gateways have failed

更难定位问题了,
所以建议还是按照教程中使用try、catch。


另外吐槽一下wsl2,每次用着用着ubuntu的时间就和windows时间(现实时间)不同步了,怀疑和笔记本的休眠有关。

《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!