On this page
对接阿里大于sdk
文档地址:
https://help.aliyun.com/document_detail/112186.html?spm=a2c4g.11174283.6.635.37eb2c42jFVxfS
(1)composer安装:
composer require alibabacloud/client
(2)使用OpenAPI Explorer来生成相关API的Demo
OpenAPI Explorer地址:
https://api.aliyun.com/?spm=a2c4g.11186623.2.13.6a294e6afatgeU#/?product=Dysmsapi&lang=PHP
(3)\app\common\controller\AliSMSController.php
php
<?php
namespace app\common\controller;
// 引入阿里sdk
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
// 引入异常类
use app\lib\exception\BaseException;
class AlismsController
{
static public function SendSMS($phone,$code){
AlibabaCloud::accessKeyClient(config('api.aliSMS.accessKeyId'),config('api.aliSMS.accessSecret'))->regionId(config('api.aliSMS.regionId'))->asGlobalClient();
try {
$option=[
'query' => [
'RegionId' => config('api.aliSMS.regionId'),
'PhoneNumbers' => $phone,
'SignName' =>config('api.aliSMS.SignName'),
'TemplateCode' =>config('api.aliSMS.TemplateCode'),
'TemplateParam' =>'{"code":"'.$code.'"}',
],
];
$result = AlibabaCloud::rpcRequest()
->product(config('api.aliSMS.product'))
// ->scheme('https') // https | http
->version(config('api.aliSMS.version'))
->action('SendSms')
->method('GET')
->options($option)->request();
return $result->toArray();
} catch (ClientException $e) {
throw new BaseException(['code'=>200,'msg'=>$e->getErrorMessage(),'errorCode'=>30000]);
} catch (ServerException $e) {
throw new BaseException(['code'=>200,'msg'=>$e->getErrorMessage(),'errorCode'=>30000]);
}
}
}
(4)使用方法:
php
use app\common\controller\AliSMSController;
...
// 生成验证码
$code = random_int(1000,9999);
// 发送短信
AliSMSController::SendSMS($phone,$code);