laravel接入阿里云短信笔记!
2019-11-17 本文已影响0人
DragonersLi
composer引入包:composer require alibabacloud/client
[root@iZbp18qq1n1pli2wswopjbZ test]# composer require alibabacloud/client
Using version ^1.5 for alibabacloud/client
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.
Problem 1
- league/flysystem 1.0.53 requires ext-fileinfo * -> the requested PHP extension fileinfo is missing from your system.
- league/flysystem 1.0.53 requires ext-fileinfo * -> the requested PHP extension fileinfo is missing from your system.
- league/flysystem 1.0.53 requires ext-fileinfo * -> the requested PHP extension fileinfo is missing from your system.
- Installation request for league/flysystem (locked at 1.0.53) -> satisfiable by league/flysystem[1.0.53].
To enable extensions, verify that they are enabled in your .ini files:
- /www/server/php/73/etc/php.ini
You can also run `php --ini` inside terminal to see which files are used by PHP in CLI mode.
Installation failed, reverting ./composer.json to its original content.
[root@iZbp18qq1n1pli2wswopjbZ test]#
宝塔上对应php版本安装fileinfo,不是php.ini开启
重启php,重新执行:composer require alibabacloud/client
image.png
The Process class relies on proc_open, which is not available on your PHP installation.
需要支持的proc_open
、proc_get_status
函数在php.ini
的disable_functions
里。从中删除这两个函数。重启php即可。
image.png
Writing lock file
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover --ansi
Installation failed, reverting ./composer.json to its original content.
[Symfony\Component\Process\Exception\RuntimeException]
The Process class relies on proc_open, which is not available on your PHP installation.
require [--dev] [--prefer-source] [--prefer-dist] [--no-progress] [--no-suggest] [--no-update] [--no-scripts] [--update-no-dev] [--update-with-dependencies] [--update-with-all-dependencies] [--ignore-platform-reqs] [--prefer-stable] [--prefer-lowest] [--sort-packages] [-o|--optimize-autoloader] [-a|--classmap-authoritative] [--apcu-autoloader] [--] [<packages>]...
[root@iZbp18qq1n1pli2wswopjbZ test]#
完成以上操作,重新执行composer require alibabacloud/client
终于ok了
[root@iZbp18qq1n1pli2wswopjbZ test]# composer require alibabacloud/client
Using version ^1.5 for alibabacloud/client
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover --ansi
Discovered Package: arrilot/laravel-widgets
Discovered Package: beyondcode/laravel-dump-server
Discovered Package: fideloper/proxy
Discovered Package: intervention/image
Discovered Package: larapack/voyager-hooks
Discovered Package: laravel/tinker
Discovered Package: nesbot/carbon
Discovered Package: nunomaduro/collision
Discovered Package: tcg/voyager
Package manifest generated successfully.
[root@iZbp18qq1n1pli2wswopjbZ test]#
阿里云短信后台设置,获取必传参数
创建AccessKey后可获取参数accessKeyId
和accessSecret
image.png
签名管理获取SignName
image.png
模板管理获取TemplateCode
image.png
可视化测试时提示模板变量缺少对应参数值
image.png
{
"Message": "模板变量缺少对应参数值",
"RequestId": "2F02B9EF-55B2-49DA-90A4-C6A735E14A68",
"Code": "isv.TEMPLATE_MISSING_PARAMETERS"
}
代码调用也是提示这个
Array (
[Message] => 模板变量缺少对应参数值
[RequestId] => C9DD5C79-60A0-4426-A49D-0460AD236409
[Code] => isv.TEMPLATE_MISSING_PARAMETERS
)
原来时调用时候模板里的${code}
没传的原因:
image.png
.env
配置阿里云短信需要的参数
ALIYUN_HOST=dysmsapi.aliyuncs.com
ALIYUN_VERSION=2017-05-25
ALIYUN_REGION_ID=cn-hangzhou
ALIYUN_TEMPLATE_CODE=SMS_177545068
ALIYUN_SIGN_NAME=xxxx短信验证码
ALIYUN_ACCESS_KEY_ID=xxxxxxxxxxxxxxx
ALIYUN_ACCESS_KEY_SECRET=xxxxxxxxxxxxxxxxxx
控制器使用demo
use Illuminate\Support\Facades\Session;
############阿里云短信#############
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
// Download:https://github.com/aliyun/openapi-sdk-php
// Usage:https://github.com/aliyun/openapi-sdk-php/blob/master/README.md
############阿里云短信#############
/*
* 发送短信
*/
public function smsSend(Request $request){
$data['mobile'] = $request->mobile ?? '18858287938';
$code = mt_rand(100000,999999);
Session::put("seavisa_smscode_wap.{$data['mobile']}",$code);
$validator = Validator($data, [
'mobile' => 'required|regex:/^1[1-9][0-9]{9}$/'
], [
'mobile.required' => '请填写手机号码',
'mobile.regex' => '手机号码无效',
]);
if ($validator->passes()) {
AlibabaCloud::accessKeyClient(env('ALIYUN_ACCESS_KEY_ID'), env('ALIYUN_ACCESS_KEY_SECRET'))
->regionId(env('ALIYUN_REGION_ID'))
->asDefaultClient();
try {
$result = AlibabaCloud::rpc()
->product('Dysmsapi')
// ->scheme('https') // https | http
->version(env('ALIYUN_VERSION'))
->action('SendSms')
->method('POST')
->host(env('ALIYUN_HOST'))
->options([
'query' => [
'RegionId' => env('ALIYUN_ACCESS_KEY_ID'),
'SignName' => env('ALIYUN_SIGN_NAME'), //必填项 签名(需要在阿里云短信服务后台申请)
'TemplateCode' => env('ALIYUN_TEMPLATE_CODE'),//必填项 短信模板code (需要在阿里云短信服务后台申请)
'TemplateParam' => "{'code':$code}",//如果在短信中添加了${code} 变量则此项必填 要求为JSON格式
'PhoneNumbers' => $data['mobile'], //需要发送到那个手机
],
])
->request();
if($result->Code == 'OK'){
return response()->json(['code'=>200,'msg'=>'验证码已发送','data'=>[]]);
}else{
return response()->json(['code'=>400,'msg'=>'验证码发送失败','data'=>[]]);
}
} catch (ClientException $e) {
return response()->json(['code'=>400,'msg'=>$e->getErrorMessage() . PHP_EOL,'data'=>[]]);
} catch (ServerException $e) {
return response()->json(['code'=>400,'msg'=>$e->getErrorMessage() . PHP_EOL,'data'=>[]]);
}
}else{
return response()->json(['code'=>400,'msg'=>$validator->messages()->first(),'data'=>[]]);
}
}
/*
* 验证验证码
*/
public function smsCheck(Request $request){
$data = $request->all();
$code = Session::get("seavisa_smscode_wap.{$data['mobile']}");
if($code == $data['code']){
return response()->json(['code'=>200,'msg'=>'验证码正确','data'=>[]]);
}else{
return response()->json(['code'=>400,'msg'=>'验证码不正确','data'=>[]]);
}
}
终于短信接收ok了
Array (
[Message] => OK
[RequestId] => 71F6C943-6B5E-4D43-AA9A-0DFACDA9CD30
[BizId] => 189407773905383695^0
[Code] => OK
)