@knife-sy
/config/easysms.php
<?php
return [
// HTTP 请求的超时时间(秒)
'timeout' => 5.0,
// 默认发送配置
'default' => [
// 网关调用策略,默认:顺序调用
'strategy' => \Overtrue\EasySms\Strategies\OrderStrategy::class,
// 默认可用的发送网关
'gateways' => [
'qcloud',
],
],
// 可用的网关配置
'gateways' => [
'errorlog' => [
'file' => '/tmp/easy-sms.log',
],
'qcloud' => [
'sdk_app_id' => env('QCLOUD_SMS_SDK_APP_ID'),
'app_key' => env('QCLOUD_SMS_APP_KEY'),
],
],
];
.env
# qcloud sms
QCLOUD_SMS_SDK_APP_ID=your_id
QCLOUD_SMS_APP_KEY=your_key
@Rainbow9502
你可能得自己去翻翻插件的文档或者源码,或者对照腾讯的官方文档看一下。
VerificationCodesController.php
<?php
namespace App\Http\Controllers\Api;
use Illuminate\Http\Request;
use App\Http\Requests\Api\VerificationCodeRequest;
use Overtrue\EasySms\EasySms;
class VerificationCodesController extends Controller
{
public function store(VerificationCodeRequest $request, EasySms $easy_sms)
{
$captcha_data = \Cache::get($request->captcha_key);
if (!$captcha_data) {
return $this->response->error('图片验证码已失效', 422);
}
if (!hash_equals($captcha_data['code'], $request->captcha_code)) {
// 验证错误就清除缓存
\Cache::forget($request->captcha_key);
return $this->response->errorUnauthorized('验证码错误');
}
$phone = $captcha_data['phone'];
$expire_minutes = 10;
if (!app()->environment('production')) {
$code = '1234';
} else {
$code = str_pad(random_int(1, 9999), 4, 0, STR_PAD_LEFT);
try {
$result = $easy_sms->send($phone, [
'content' => " {$code}为您的登录验证码,请于{$expire_minutes}分钟内填写。如非本人操作,请忽略本短信。 ",
]);
} catch (\GuzzleHttp\Exception\ClientException $e) {
$response = $exception->getResponse();
$result = json_decode($response->getBody()->getContents(), true);
return $this->response->errorInternal($result['msg'] ?? '短信发送异常');
}
}
$key = 'verificationCode_' . str_random(15);
$expired_at = now()->addMinutes($expire_minutes);
// 缓存验证码 10 分钟过期
\Cache::put($key, ['phone' => $phone, 'code' => $code], $expired_at);
return $this->response->array([
'key' => $key,
'expired_at' => $expired_at->toDateTimeString(),
])->setStatusCode(201);
}
}
@truesnow 已经申请好了鹅厂的短信,也拿到了 APP_ID,APP_KEY。在执行 php artisan tinker 时,使用
$sms = app('easysms');
=> Overtrue\EasySms\EasySms {#1082}
try {
... $sms->send(电话号码, [
... 'content' => '【Lbbs社区】您的验证码是1234。如非本人操作,请忽略本短信',
... ]);
... } catch (\Overtrue\EasySms\Exceptions\NoGatewayAvailableException $exception) {
... $message = $exception->getException('qcloud')->getMessage();
... dd($message);
... }
"签名格式错误或者签名未审批"
查了相关资料。发现国内好多短信 签名都需要认证备案。 无奈之下,只有先放弃了。
我的片云没有通过,申请了腾讯云。然后遇到和@Oyxiaoxi 一样的问题 签名需要认证备案。在使用网易云,结果还是不行。于是返回使用腾讯云,去申请。
只要个人有小程序、公众号或者有网站都行。公众号不一定要认证,只要能搜索到就行(客服和我说的)。然后申请完了就可以使用了。个人还是推荐使用腾讯云的。
推荐文章: