Laravel-手机短信验证码-阿里云
作者:互联网
1.composer require alibabacloud/client
2.App\Service\AliyunSms.php
<?php namespace App\Service; use AlibabaCloud\Client\AlibabaCloud; use AlibabaCloud\Client\Exception\ClientException; use AlibabaCloud\Client\Exception\ServerException; class AliyunSms { public static function sendCode($phone, $code) { AlibabaCloud::accessKeyClient('LTAIbuiQwJNOUJ4l', 'og3fSMFpYZMkPB0Ulgcd44JnUAZnEQ') ->regionId('cn-hangzhou') ->asDefaultClient(); try { $result = AlibabaCloud::rpc(['idn_conversion' => false]) ->product('Dysmsapi') ->version('2017-05-25') ->action('SendSms') ->method('POST') ->host('dysmsapi.aliyuncs.com') ->options([ 'query' => [ 'RegionId' => "cn-hangzhou", 'PhoneNumbers' => $phone, 'SignName' => "青创", 'TemplateCode' => "SMS_96700063", 'TemplateParam' => "{code:{$code}}", ], ])->request(); return $result; }catch (ClientException $e){ print_r($e->getMessage()); } } }
3.控制器调用测试
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Service\AliyunSms; class TestController extends Controller { //短信测试 public function sendSms(){ $phone = '1851850****'; $code = rand(111111,999999); $result = AliyunSms::sendCode($phone,$code); if($result){ echo "短信发送成功"; }else{ echo "失败"; } } }
4.路由定义 api.php
Route::get('/test/sendSms','TestController@sendSms');
标签:Laravel,code,cn,sendSms,验证码,手机短信,hangzhou,result,php 来源: https://www.cnblogs.com/superzwb/p/14842787.html