How to implement the thinkphp 5 in Ali communication text message

Source: Internet
Author: User
Tags aliyun

How to implement Aliyun-cloud communication in thinkphp 5

Ali is greater than the consolidation of Aliyun, Ali is greater than the maintenance of original customers. New customers can buy SMS services in Aliyun-Cloud communications. First we can look at the menu location of Aliyun-Cloud communication:

After adding the SMS service, you can see the menu of the SMS service on the left:

Use Aliyun-cloud communication to pay attention to three places: 1 set eccesskeys--This is the SMS Service account and password. 2) Set SMS signature. 3 Set SMS template. Because Aliyun-Cloud communication itself has the use of tutorials and customer service, how to apply and use we do not too much to tell, below we download a thinkphp 5 based Wstmart Open source e-business system to explain: how to use and implement thinkphp 5 in the framework of access to Aliyun-cloud communication functions.

1, in Wstmart "Expand Management"-"plug-in management" to find "SMS interface (Aliyun-Cloud communication)" Plug-in, click on the right side of the "Installation" to enable. Then set up the Aliyun-cloud communication in the settings interface:


The access key ID, access key secret, and SMS signature above must be filled in. The template ID that is sent is selected, and if the user is not filled in, the system does not send a text message in that case.

2, in the mall set up to open the message function:


To be aware of, Wstmart in the default SMS send interval is 2 minutes, Aliyun-cloud communication is the shortest 1 minutes 1, 1 hours 5, 1 days 10. Set up here, basically wstmart can use the function of SMS service.

3. Principle of realization:

1) Download Aliyun-Cloud Communications SDK into the Aliyun-Cloud Communications Plug-in directory, the name of the SDK.

2 in the location of text messages buried hooks. In the class file wstmart\common\model\logsms.php we bury the hooks in the location where we send the text messages.


When the program is running here, the system automatically detects what plug-ins are listening on at this hook. Because we only have Aliyun-cloud communication in front of us, the program will go into the plug-in function of Aliyun-cloud communication:


Sendsms written in the plug-in model, we follow the Aliyun-Cloud communication demo code encapsulated into an HTTP interface function:

Public function http ($params) {require_once wst_addon_path. ' dysms/sdk/vendor/autoload.php ';
                 Config::load ();
            $smsConf = $this->getconfigs ();
            $accessKeyId = $smsConf [' Smskey '];;
            $accessKeySecret = $smsConf [' Smspass '];
            $product = "Dysmsapi";
            $domain = "dysmsapi.aliyuncs.com";
            $region = "Cn-hangzhou";
           $profile =defaultprofile::getprofile ($region, $accessKeyId, $accessKeySecret);
            Defaultprofile::addendpoint ("Cn-hangzhou", "Cn-hangzhou", $product, $domain);
            $acsClient = new Defaultacsclient ($profile);
           $request = new Sendsmsrequest ();
            $request->setphonenumbers ($params [' PhoneNumber ']);
            Required-Short Message signature $request->setsignname ($smsConf ["signature"]);
            Required-Short Message template code $request->settemplatecode ($smsConf [$params [' params '] [' TPL '] [' Tplcode ']]); $request->settemplateparam ($params[' content ']);
            Optional fill-Send SMS serial number $request->setoutid ($params [' Timeid ']);
       Initiating an access request $RESP = $acsClient->getacsresponse ($request);
return $RESP;
  }

Then we're going to call the HTTP function in model to send text messages.

Public Function sendsms ($params) {
                 ...
       .. Omitting the encapsulation Function procedure
                 .... $code = $this->http ($params);
                 $log = Model (' common/logsms ')->get ($params [' smsid ']);
                 $log->smsreturncode= Json_encode ($code);
                 $log->save ();
                 try{
                         if (strtolower ($code->message) = = ' OK ') {
                    $params [' status '] [' msg '] = ' SMS sent successfully! ';
                    $params [' status '] [' status '] = 1;
                         }
                 } catch (\exception $e) {
           $params [' status '] [' msg '] = $code->message;
                $params [' status '] [' status '] =-1;
                 }
}
 


Debugging: If the call to Aliyun-cloud traffic returns the data of the JSON object correctly, and its message is OK. It means that the message is successful, otherwise it prompts the user to send the message failed.

Welcome to share and exchange with us.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.