以PHP版為例
1.本地項目中安裝安裝PHP SDK
(1)composer安裝:
https://help.aliyun.com/document_detail/112186.html?spm=a2c4g.11174283.6.635.37eb2c42jFVxfS
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
2.阿里大魚簽名和SMS碼獲取
二.阿里大魚驗證碼使用
1.獲取的代碼格式如下并不能直接使用,而是需要進行修改
<?php
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.mdAlibabaCloud::accessKeyClient('<accessKeyId>', '<accessSecret>')->regionId('cn-hangzhou')->asDefaultClient();try {$result = AlibabaCloud::rpc()->product('Dysmsapi')// ->scheme('https') // https | http->version('2017-05-25')->action('SendSms')->method('POST')->host('dysmsapi.aliyuncs.com')->options(['query' => ['RegionId' => "cn-hangzhou",'PhoneNumbers' => "手機號",'SignName' => "簽名",'TemplateCode' => "簽名SMS碼",'TemplateParam' => "驗證碼格式{\"code\":1111}",],])->request();print_r($result->toArray());
} catch (ClientException $e) {echo $e->getErrorMessage() . PHP_EOL;
} catch (ServerException $e) {echo $e->getErrorMessage() . PHP_EOL;
}
2.單獨創建一個控制器文件,封裝修改阿里大魚
<?phpnamespace 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'))->asDefaultClient();try {//定義自己的手機號碼,簽名,SMS碼,驗證碼格式$option=['query' => ['RegionId' => config('api.aliSMS.regionId'),'PhoneNumbers' => $phone,'SignName' =>config('api.aliSMS.SignName'),'TemplateCode' =>config('api.aliSMS.TemplateCode'),'TemplateParam' =>'{"code":"'.$code.'"}',],];$result = AlibabaCloud::rpc()->product(config('api.aliSMS.product'))// ->scheme('https') // https | http->version(config('api.aliSMS.version'))->action('SendSms')->method('POST')->host('dysmsapi.aliyuncs.com')->options($option)->request();// print_r($result->toArray());//返回數據return $result->toArray();} catch (ClientException $e) {echo $e->getErrorMessage() . PHP_EOL;//使用封裝的異?;悘棾鲥e誤throw new BaseException(['code'=>200,'msg'=>$e->getErrorMessage(),'errorCode'=>30000]);} catch (ServerException $e) {throw new BaseException(['code'=>200,'msg'=>$e->getErrorMessage(),'errorCode'=>30000]);}}}
3.將配置信息提取到單獨的配置文件中,便于后期維護
4.模型中引用
<?php
namespace app\common\model;use think\Model;
// 引入TP5的緩存類
use think\facade\Cache;
// 引入異?;?
use app\lib\exception\BaseException;
//引入阿里大魚驗證碼類
use app\common\controller\AliSMSController;class User extends Model
{//發送驗證碼public function sendCode(){// 獲取用戶提交手機號碼$phone = request()->param('phone');// 判斷是否已經發送過//如果可以從緩存中讀取手機驗證碼if(Cache::get($phone)) throw new BaseException(['code'=>200,'msg'=>'你操作得太快了','errorCode'=>30001]); //生成四位數字的驗證碼$code = random_int(1000,9999);//使用阿里大魚發送驗證碼// 發送驗證碼$res = AlismsController::SendSMS($phone,$code);//發送成功 寫入緩存if($res['Code']=='OK') return Cache::set($phone,$code,config('api.aliSMS.expire'));// 無效號碼if($res['Code']=='isv.MOBILE_NUMBER_ILLEGAL') throw new BaseException(['code'=>200,'msg'=>'無效號碼','errorCode'=>30002]);// 觸發日限制if($res['Code']=='isv.DAY_LIMIT_CONTROL') throw new BaseException(['code'=>200,'msg'=>'今日你已經發送超過限制,改日再來','errorCode'=>30003]);// 發送失敗throw new BaseException(['code'=>200,'msg'=>'發送失敗','errorCode'=>30004]);}}
?