2023年7月31日发(作者:)
基于thinkphp6的阿⾥云短信发送接⼝####1 ⾸先获取阿⾥云PHP SDKcomposer require alibabacloud/sdk###2 编写接⼝
阿⾥短信发送服务类 */declare(strict_types=1);namespace appcommonservicelibsmsalisms;use AlibabaCloudclientAlibabaCloud;use AlibabaCloudclientClientException;use AlibabaCloudclientServerException;class AliSms{ /** * @param string $phone
⼿机号码 * @param int $code
验证码 * @return bool true */ public static function send(string $phone, int $code): bool { if (empty($phonr) || empty($code)) { return false; } try { $templateParam = json_encode(['code' => $code]); //
创建客户端 //foo:此处填写⾃⼰的accessKey和bar:accessKeySecret AlibabaCloud::accessKeyClient(config('_key_id'), config('_key_secret')) ->regionId(config('_id')) ->asDefaultClient(); //
发送请求 $result = AlibabaCloud::rpc() ->product('Dysmsapi') // ->scheme('https') // https | http ->version('2017-05-25') ->action('SendSms') ->method('POST') ->host(config('')) ->options([ 'query' => [ 'RegionId' => config('_id'), //需要发送到那个⼿机 'PhoneNumbers' => $phone, //必填项
签名(需要在阿⾥云短信服务后台申请) 'SignName' => config('_name'), //必填项
短信模板code (需要在阿⾥云短信服务后台申请) 'TemplateCode' => config('te_code'), //如果在短信中添加了${code}
变量则此项必填
要求为JSON格式 'TemplateParam' => $templateParam, ], ]) ->request(); //将结果打印出来看⼀哈 //将结果打印出来看⼀哈 print_r($result->toArray()); } catch (ServerException $e) { // Get server error message print_r($e->getErrorMessage()); } catch (ClientException $e) { // Get client error message print_r($e->getErrorMessage()); } return true; }}接⼝调⽤ public function sends(string $phone): bool { $code = rand(100000, 999999); //
长度 $sms = AliSms::send($phone, $code); if ($sms) { //发送成功,则需要我们将短信验证码存在Redis⾥⾯,并且给出失效时间 }else{ } return true; }
2023年7月31日发(作者:)
基于thinkphp6的阿⾥云短信发送接⼝####1 ⾸先获取阿⾥云PHP SDKcomposer require alibabacloud/sdk###2 编写接⼝
阿⾥短信发送服务类 */declare(strict_types=1);namespace appcommonservicelibsmsalisms;use AlibabaCloudclientAlibabaCloud;use AlibabaCloudclientClientException;use AlibabaCloudclientServerException;class AliSms{ /** * @param string $phone
⼿机号码 * @param int $code
验证码 * @return bool true */ public static function send(string $phone, int $code): bool { if (empty($phonr) || empty($code)) { return false; } try { $templateParam = json_encode(['code' => $code]); //
创建客户端 //foo:此处填写⾃⼰的accessKey和bar:accessKeySecret AlibabaCloud::accessKeyClient(config('_key_id'), config('_key_secret')) ->regionId(config('_id')) ->asDefaultClient(); //
发送请求 $result = AlibabaCloud::rpc() ->product('Dysmsapi') // ->scheme('https') // https | http ->version('2017-05-25') ->action('SendSms') ->method('POST') ->host(config('')) ->options([ 'query' => [ 'RegionId' => config('_id'), //需要发送到那个⼿机 'PhoneNumbers' => $phone, //必填项
签名(需要在阿⾥云短信服务后台申请) 'SignName' => config('_name'), //必填项
短信模板code (需要在阿⾥云短信服务后台申请) 'TemplateCode' => config('te_code'), //如果在短信中添加了${code}
变量则此项必填
要求为JSON格式 'TemplateParam' => $templateParam, ], ]) ->request(); //将结果打印出来看⼀哈 //将结果打印出来看⼀哈 print_r($result->toArray()); } catch (ServerException $e) { // Get server error message print_r($e->getErrorMessage()); } catch (ClientException $e) { // Get client error message print_r($e->getErrorMessage()); } return true; }}接⼝调⽤ public function sends(string $phone): bool { $code = rand(100000, 999999); //
长度 $sms = AliSms::send($phone, $code); if ($sms) { //发送成功,则需要我们将短信验证码存在Redis⾥⾯,并且给出失效时间 }else{ } return true; }
发布评论