Tihinkphp3.2 integrate the latest version of Alibaba fish to send SMS verification code

Posted by Neomech on Fri, 20 Mar 2020 16:43:03 +0100

Alida fish's latest download address: Alibig fish SDK Download Or download from the official website: Alibaba big fish SDK official website download

After downloading, put the API ﹣ SDK folder in the compressed package into the ThinkPHP\Library\Vendor directory, and change the file name to Aliyun. If you want to change it to another name, please change the namespace!

Alida fish application steps will not be mentioned! Let's start connecting~

Set the configuration information of Alibaba big fish:

Configure in profile:

 1     'ALI_SMS' => [
 2         'PRODUCT' => 'Dysmsapi',
 3         'DOMAIN' => 'dysmsapi.aliyuncs.com',
 4         'REGION' => 'cn-hangzhou',
 5         'END_POINT_NAME' => 'cn-hangzhou',
 6         'KEY_ID' => 'Your Access Key ID',
 7         'KEY_SECRET' => 'Your Access Key Secret',
 8         'SIGN_NAME' => 'Signature name',
 9         'TEMPLATE_CODE' =>'Template code',
10     ],

 

After setting, create a new SMS sending function in function.php file, which is named sendCode:

<?php
use Aliyun\Core\Config;
use Aliyun\Core\Profile\DefaultProfile;
use Aliyun\Core\DefaultAcsClient;
use Aliyun\Api\Sms\Request\V20170525\SendSmsRequest;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/2 0002
 * Time: 10:20
 */

function sendCode($num,$code){
    require_once '/ThinkPHP/Library/Vendor/Aliyun/vendor/autoload.php';
    //Load area node configuration
    Config::load();
    // Initialize users Profile Example
    $profile = DefaultProfile::getProfile(C('ALI_SMS.REGION'), C('ALI_SMS.KEY_ID'), C('ALI_SMS.KEY_SECRET'));
    // Add service node
    DefaultProfile::addEndpoint(C('ALI_SMS.END_POINT_NAME'), C('ALI_SMS.REGION'), C('ALI_SMS.PRODUCT'), C('ALI_SMS.DOMAIN'));
    // Initialization AcsClient For request initiation
    $acsClient= new DefaultAcsClient($profile);
    // Initialization SendSmsRequest Instance is used to set parameters for sending SMS
    $request = new SendSmsRequest();
    // Required, set the receiving number of pheasant SMS
    $request->setPhoneNumbers($num);
    // Required, set signature name
    $request->setSignName(C('ALI_SMS.SIGN_NAME'));
    // Required, set template CODE
    $request->setTemplateCode(C('ALI_SMS.TEMPLATE_CODE'));
    // Optional, set template parameters
    $request->setTemplateParam(json_encode(array(  // Value of field in SMS template
        "code"=>$code,
        "product"=>"dsd"
    ), JSON_UNESCAPED_UNICODE));
    //Initiate access request
    $acsResponse = $acsClient->getAcsResponse($request);
    //Return request results
    $result = json_decode(json_encode($acsResponse),true);
    return $result;

}

 

Use only calling = sendCode in the controller:

public function sendmsg(){
    $phone = input('post.phone_num');
    $code = rand(100000,999999);
    $res = sendCode($phone,$code);
    if($code == "OK"){
        $data = array('msg'=>'success');
    }else{
        $data = array('msg'=>'error');
    }
    return json_encode($data);
}

 

The effect is as follows:

Original address: Mr. Zhang's blog      http://www.zhangfayuan.cn/archives/201805041200198.html

Topics: PHP SDK PhpStorm