tp5 Ali Cloud SMS verification code

1. After Ali cloud account login to create the first mouse to move after clicking on your avatar AccessKey

2. Locate the SMS service here and signed application template

3. After the find help documentation -> Download php SDK

 I clicked Development Guide (legacy) download php SDK non-lightweight version of

After downloading codecs extend / alisms 

After the app \ common.php under

Paste the code

? < PHP
 use Aliyun \ Core \ Config;
 use Aliyun \ Core \ Profile \ the DefaultProfile;
 use Aliyun \ Core \ DefaultAcsClient;
 use Aliyun \ Api \ Sms \ Request \ V20170525 \ SendSmsRequest;
 / * * 
 * SMS 
 * @param $ to recipients 
 * @param $ code verification SMS code 
 * JSON @return 
 * / 
function send_sms ( $ to , $ code ) {
     require_once '../extend/alisms/api_sdk/vendor/autoload.php' ; 
    Config :: Load () ; // loading junction region disposed 
    $ accessKeyId = 'the first step in creating the accessKey' ;
     $ accessKeySecret= 'First step in creating a accessKeySecret' ;
     $ templateParam = $ code ;
     // SMS API product name (product name fixed SMS, without modification) 
    $ Product = "Dysmsapi" ;
     // SMS API product domain name (interface address fixed without modification) 
    $ Domain = "dysmsapi.aliyuncs.com" ;
     // does not support multi Region (currently only supports cn-hangzhou Do not modify) 
    $ Region = "cn-hangzhou" ;
     // Profile Initializes an instance of the user 
    $ Profile = the DefaultProfile GetProfile :: ( $ Region , $ accessKeyId , $ accessKeySecret );
     // increase the service node 
    DefaultProfile :: addEndpoint ( "cn-hangzhou ", "cn-hangzhou", Product $ , $ Domain );
     // initialize AcsClient initiate a request for 
    $ acsClient = new new DefaultAcsClient ( $ Profile );
     // initialize SendSmsRequest examples of setting parameters to send SMS 
    $ Request = new new SendSmsRequest ();
     // required, set pheasant SMS number to receive 
    $ Request -> setPhoneNumbers ( $ to );
     // required, set the signature name 
    $ Request -> setSignName ( "your signature name" );
     // required, set the template CODE 
    $ Request -> setTemplateCode ( "your template code" );
     // optional, set the template parameter 
    IF ( $ templateParam) {
         $ Request -> setTemplateParam (json_encode ([ 'code' => $ templateParam ])); // Since I need to pass the template I message authentication code 
    } 
    // initiate an access request 
    $ acsResponse = $ acsClient -> getAcsResponse ( $ request );
     // returns the result of the request 
    $ the result = json_decode (json_encode ( $ acsResponse ), to true );
     // specific return value of the reference document: https: //help.aliyun.com/document_detail/55451.html spm =? a2c4g.11186623.6.563.YSe8FK 
    return  $ the Result ; 
}
 >?
<? PHP
 public  function code () {
         $ code = RAND (100000,999999 );
         $ Phone = "fill in your phone number" ;
         $ the Result = send_sms ( $ Phone , $ code );
         var_dump ( $ the Result ); 
    }

 ? >

After testing on it

Guess you like

Origin www.cnblogs.com/buxin111/p/12600148.html