Background -- tp SMS verification (configuration and use)

Keywords: Mobile Database PHP Excel

Alicloud SMS verification code

Today, we have a verification code for SMS, which is based on Alibaba cloud platform, https://cn.aliyun.com/ss/?k=%E7%9F%AD%E4%BF%A1api , free verification code test, no charge. There are many free testing interfaces for alicloud's api (in fact, if you pay one yuan, it means you use it, he will judge that if you have money in alicloud, you will automatically deduct the fee. One yuan is enough for you to test)

Other people's api

 

Buy:

 

Point him:

 

Information of SMS configuration we want:

 

Configure alicloud

 

Search to open it:

 

You can also click to see the domestic news to add

 

What's the use of us

First: signature name

 

That's our company name. Of course, it's in the code. Write it first, and then you will know where it is in the code. Of course, it needs to be reviewed, and it can be used after passing the review

Add template to:

 

Take a look at the contents of the template:

Then you can just say something about the application description. Pay attention to the variable name given by him. Try not to change it, because the variable above their instance is the same. It's OK to submit it for review.

 

Own api

The configuration is the same as above

 

Alibaba cloud api configuration

 

Search to open it:

 

You can also click to see the domestic news to add

 

What's the use of us

First: signature name

 

That's our company name. Of course, it's in the code. Write it first, and then you will know where it is in the code. Of course, it needs to be reviewed, and it can be used after passing the review

Add template to:

 

Take a look at the contents of the template:

Then you can just say something about the application description. Pay attention to the variable name given by him. Try not to change it, because the variable above their instance is the same. It's OK to submit it for review.

 

 

We need to generate our own

$accessKeyId and $accessKeySecret

 

General information:

 

 

Click:

 

 

 

 

Click create to:

 

 

Your phone verification code

 

 

 

Then he asks you to download an excel file with

 

Don't worry about this. Put it in a safe place. You need to use

 

 

 

 

Example analysis:

Example introduction: first get the mobile phone number, input it correctly to judge, then I inserted the verification code information into our database.

 

The premise of insertion is that the mobile number is entered correctly, and the verification code has been sent to your mobile phone.

 

When your verification code is entered correctly, I will prompt you to register successfully, within the validity period of this verification code.

I set the validity period to five minutes

 

 

Configuration code

I use my own domain name instead of someone else's:

We can write a dollar directly on Alibaba cloud and send a text message

 

 

Put the downloaded file in extend and change its name to alisms

 

 

I put the file in the public part, which is convenient for us to call

 

 

Two parameters that must be passed

 

What to change in the public file

 

 

In Index controller

Call it once

 

Code sharing done above

Good code, database and judgment

Links: https://pan.baidu.com/s/1Yg1p7ilfVYZ7IMQcwBnLhg

Extraction code: vkz6

 

 

If you don't want to, I'll give you an empty demo

Links: https://pan.baidu.com/s/1mW5wQ--GwPkPb0loxSmyHQ 
Extraction code: qcki

Code sharing:

Note: we need to put the downloaded content in the extend folder and change its name to alisms;

common function code

<?php
use Aliyun\Core\Config;
use Aliyun\Core\Profile\DefaultProfile;
use Aliyun\Core\DefaultAcsClient;
use Aliyun\Api\Sms\Request\V20170525\SendSmsRequest;
/**
 * SMS sending
 * @param $to    Receiver
 * @param $code   SMS verification code
 * @return json
 */
function send_sms($to, $code){
    require_once '../extend/alisms/api_sdk/vendor/autoload.php';
    Config::load(); //Load area node configuration
    $accessKeyId = '******************';//Own AccessKeyId
    $accessKeySecret = '**************';//Own AccessKeySecret
    $templateParam = $code;
    //SMS API product name (fixed SMS product name, no need to modify)
    $product = "Dysmsapi";
    //SMS API product domain name (fixed interface address, no need to modify)
    $domain = "dysmsapi.aliyuncs.com";
    //Multiple regions are not supported temporarily (currently only CN Hangzhou is supported, please do not modify)
    $region = "cn-hangzhou";
    // Initialize user Profile instance
    $profile = DefaultProfile::getProfile($region, $accessKeyId, $accessKeySecret);
    // Add service node
    DefaultProfile::addEndpoint("cn-hangzhou", "cn-hangzhou", $product, $domain);
    // Initialize AcsClient for request initiation
    $acsClient= new DefaultAcsClient($profile);
    // Initialize SendSmsRequest instance to set parameters for sending SMS
    $request = new SendSmsRequest();
    // Required, set the receiving number of pheasant SMS
    $request->setPhoneNumbers($to);
    // Required, set signature name
    $request->setSignName("********");//To change
    // Required, set template CODE
    $request->setTemplateCode("*********");//To change
    // Optional, set template parameters
    if($templateParam) {
        $request->setTemplateParam(json_encode(['code'=>$templateParam]));//Because my template needs to pass my SMS verification code
    }
    //Initiate access request
    $acsResponse = $acsClient->getAcsResponse($request);
    //Return request results
    $result = json_decode(json_encode($acsResponse),true);
    // Refer to the document for specific return value: https://help.aliyun.com/document_detail/55451.html?spm=a2c4g.11186623.6.563.YSe8FK
    return $result;
}
?>

 

index controller

 public function verification()
 {
        $code = rand( 100000, 999999 );
        $phone = '150*****';//Correct phone number
        $result = send_sms( $phone, $code );
        dump($result);exit;
}

 

Published 127 original articles· Zan Zan 18. 20000 visitors+
Private letter follow

Posted by steve012345 on Fri, 17 Apr 2020 21:55:03 -0700