The use of tp5 SMS interface

The SMS configuration of Alibaba Big Fish is very simple. Just import the complete SDK of Alibaba Big Fish, and then reference the autoload.php file in the SDK directory in your own project, and then call the demo interface on the interface.

First, let’s talk about the specific implementation method, and finally add the source code package!

1. Download the SDK, the interface is on the official website of Ali Dayu. The version I use is downloaded in October 2017. I do not guarantee that the official SDK will be the same in the future. The address of my SDK version is Baidu Cloud  : oy7c; after downloading, put it under the TP5 extension directory extend, as shown in the figure:

2. Write your own SMS sending interface following the demo of the SDK. The premise here is to introduce the SDK directory autoload.php file, the APP_EXTEND macro definition directory + SDK path; the APP_EXTEND definition location is in the entry file index.php, define('APP_EXTEND' ,__DIR__.'/extend/');

 

 

After the definition, we can introduce the SDK loading path. We create a new sms controller in the home controller, and then introduce the namespace required by the SDK as shown in the figure.

 

 

At this point, the introduction of the SDK is complete, and the rest is to copy the demo function of the sdk and build your own startSendSms() sending function.

The following is the SMS interface code, netizens who need it can download it directly  :

 

namespaceapp\home\controller;

usethink \ Controller;

usethink \ Session;

ini_set("display_errors","on");

require_onceAPP_EXTEND.'Alidayu/api_sdk/vendor/autoload.php';

useAliyun\Core\Config;

useAliyun\Core\Profile\DefaultProfile;

useAliyun\Core\DefaultAcsClient;

useAliyun\Api\Sms\Request\V20170525\SendSmsRequest;

useAliyun\Api\Sms\Request\V20170525\QuerySendDetailsRequest;

//Load the region node configuration

Config::load();

/**

* Class SmsDemo

*

*@property\Aliyun\Core\DefaultAcsClient acsClient

*/

classSmsextendsController

{

/**

* Constructor

*

*@paramstring $accessKeyId is required, AccessKeyId

*@paramstring $accessKeySecret is required, AccessKeySecret

*/

public function__construct($accessKeyId='write your own SMS accessKeyId here', $accessKeySecret='write your own SMS accessKeySecret here')

{

//SMS API product name

$product="Dysmsapi";

//SMS API product domain name

$domain="";

//Multiple Regions are not supported for the time being

$region="cn-hangzhou";

//service node

$endPointName="cn-hangzhou";

//Initialize the user Profile instance

$profile=DefaultProfile::getProfile($region,$accessKeyId,$accessKeySecret);

//Add service node

DefaultProfile::addEndpoint($endPointName,$region,$product,$domain);

//Initialize AcsClient to initiate the request

$this->acsClient=newDefaultAcsClient($profile);

}

/**

*Send SMS example

*

*@paramstring $signName

*Required, SMS signature, should be filled in strictly "signature name", refer to: SMS signature page

*

*@paramstring $templateCode

*Required, SMS template code, should be filled in strictly according to "template code", refer to: SMS template page

* (e.g. SMS_0001)

*

*@paramstring $phoneNumbers is required, SMS receiving number (eg 12345678901)

*@paramarray|null $templateParam

*Optional, if there are variables in the template that need to be replaced, it is required (eg Array("code"=>"12345", "product"=>"Ali Communication"))

*

*@paramstring|null $outId [optional] optional, send SMS serial number (eg 1234)

*@returnstdClass

*/

public functionsendSms($signName,$templateCode,$phoneNumbers,$templateParam,$outId=null){

//Initialize the SendSmsRequest instance to set the parameters for sending SMS

$request=newSendSmsRequest();

//Required, set the pheasant SMS receiving number

$request->setPhoneNumbers($phoneNumbers);

//Required, set the signature name

$request->setSignName($signName);

//Required, set template CODE

$request->setTemplateCode($templateCode);

//Optional, set template parameters

if($templateParam){

$request->setTemplateParam(json_encode($templateParam));

}

//Optional, set the serial number

if($outId){

$request->setOutId($outId);

}

//Initiate an access request

$acsResponse=$this->acsClient->getAcsResponse($request);

// print the request result

// var_dump($acsResponse);

return$acsResponse;

}

//Call the send SMS interface

public function startSendSms(){ //Here is the send function written by myself, which can directly call the sendSms interface of the SDK

$signName='write signature here';//Signature

$templateCode='write SMS template here';//SMS template such as: SMS_105200000

$mobile=input('phone');//The number passed

if($mobile==""){

returnApiError('Mobile number cannot be empty');

return;

}

if(checkMobile($mobile)){

returnApiError('Mobile phone number format is incorrect');

return;

}

$phoneNumbers=$mobile;

$verifycode=strval(rand(1000,9999));//Verification code number 4-digit verification code according to your needs

$templateParam=array('code'=>$verifycode);

$result=$this->sendSms($signName,$templateCode,$phoneNumbers,$templateParam,$outId=null); //Call SDK interface

$result=json_decode(json_encode($result),true);

if($result['Message']!='OK'){

returnApiError('Verification code sending failed:'.$result['Message']);

return;

}

session('home_reg.verifycode',$verifycode);

session('home_reg.phone',$phoneNumbers);

session('home_reg.time',time()+600);//10分钟

returnApiSuccess('Verification code sent successfully, please pay attention to check');

}

}

In this way, the entire SMS configuration is completed, and the front end only needs to call your sms/startSendSms interface to send SMS! Problems that may be encountered SDK autoload.PHP is not imported correctly.

Reprinted in:

PHP programmer

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326429661&siteId=291194637