To use ali Cloud SMS API, you need to obtain the following necessary parameters on the console, which requires your own mobile phone verification + official review for several times, especially the review needs to be patient.

1. AccessKeyId is your personal account key;

AccessKeySecret is paired with accessKeySecret;

3. Personal signature, this signature will be shown at the beginning of the outgoing message, like [signature] Dear user…… In this format, SignName needs to be submitted for approval;

4.TemplateCode TemplateCode, ali cloud SMS is unable to fully customize SMS, need to pass the audit template, and then replace the variables in the template, such as template: “your verification code is ${code}”,code is the variable, use the variable value {“code” : “12345”}(the process of setting variable values is implemented in demo), after the SMS is sent, it will become: “your verification code is 12345”, each approved template will provide a template code;

The latest Ali cloud SMS interface, applicable to ali greater than moving after the situation. I have been using ali’s SMS interface before, but recently I found that Ali Has quietly moved to Ali Cloud when I went on the project! Ali cloud SDK files are various, look confused! The following code is the latest can be applied to ali cloud SMS service class!

<? PHP /** * @author Administrator ** / class Sms {$error; // Access Key ID private $accessKeyId = ''; // Access Access Key Secret private $accessKeySecret = ''; Private $signName = "; // template ID private $templateCode = "; public function __construct($cofig = array()) { $cofig = array ( 'accessKeyId' => 'xxxxxxxxxxx', 'accessKeySecret' => 'XXXXXXXXXX ', 'signName' =>' your signature ', 'templateCode' => 'SMS_76510109'); $this->accessKeyId = $cofig ['accessKeyId']; $this->accessKeySecret = $cofig ['accessKeySecret']; $this->signName = $cofig ['signName']; $this->templateCode = $cofig ['templateCode']; } private function percentEncode($string) { $string = urlencode ( $string ); $string = preg_replace ( '/\+/', '%20', $string ); $string = preg_replace ( '/\*/', '%2A', $string ); $string = preg_replace ( '/%7E/', '~', $string ); return $string; } /** * signature ** @param unknown $parameters * @param unknown $accessKeySecret * @return string */ private function computeSignature($parameters, $accessKeySecret) { ksort ( $parameters ); $canonicalizedQueryString = ''; foreach ( $parameters as $key => $value ) { $canonicalizedQueryString .= '&' . $this->percentEncode ( $key ) . '=' . $this->percentEncode ( $value ); } $stringToSign = 'GET&%2F&' . $this->percentencode ( substr ( $canonicalizedQueryString, 1 ) ); $signature = base64_encode ( hash_hmac ( 'sha1', $stringToSign, $accessKeySecret . '&', true ) ); return $signature; } /** * @param unknown $mobile * @param unknown $verify_code * */ public function send_verify($mobile, $verify_code) {$params = array ($this-> SignName, 'Format' => 'JSON', $this-> AccessKeyId, 'SignatureVersion' => $this-> AccessKeyId, 'SignatureVersion' => '1.0', 'SignatureMethod' => 'HMAC-SHA1', 'SignatureNonce' => uniqid (), 'Timestamp' => gmdate ( 'Y-m-d\TH:i:s\Z' ), 'Action' => 'SendSms', 'TemplateCode' => $this->templateCode, 'PhoneNumbers' => $mobile, / / 'TemplateParam' = > '{" code ":"'. $verify_code. '"}' 'TemplateParam' = > '{" time ":" 1234 "}' / / replacement for their actual template); //var_dump($params); die; $params ['Signature'] = $this->accessKeySecret ($params, $this->accessKeySecret); Changed it / / send the request (here). / / $url = 'https://sms.aliyuncs.com/?' http_build_query ($params); $url = 'http://dysmsapi.aliyuncs.com/?' . http_build_query ( $params ); $ch = curl_init (); curl_setopt ( $ch, CURLOPT_URL, $url ); curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, FALSE ); curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, FALSE ); curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt ( $ch, CURLOPT_TIMEOUT, 10 ); $result = curl_exec ( $ch ); curl_close ( $ch ); $result = json_decode ( $result, true ); //var_dump($result); die; if (isset ( $result ['Code'] )) { $this->error = $this->getErrorMessage ( $result ['Code'] ); return false; } return true; } @param unknown $status */ public function getErrorMessage($status) {// $message = array (https://api.alidayu.com/doc2/apiDetail?spm=a3142.7629140.1.19.SmdYoA&apiId=25450 SMS 'InvalidDayuStatus. Malformed' = > 'account opening status is not correct', 'InvalidSignName. Malformed' = > 'message signature is not correct or signature status is not correct', 'InvalidTemplateCode. MalFormed' = > 'message template Code is not correct or template condition is not correct', 'InvalidRecNum. MalFormed' = > 'target mobile phone number is not correct, Send a single number cannot exceed 100 ', 'InvalidParamString. The MalFormed' = > 'message is not a json format template variables', 'InvalidParamStringTemplate. Malformed' = > 'message template variables and template content does not match the', 'InvalidSendSms' = >' trigger business flow control, 'InvalidDAYu. Malformed' =>' Variable cannot be a URL, variable can be solidified in template '); if (isset ( $message [$status] )) { return $message [$status]; } return $status; }}Copy the code

Call method:

$mobile = 'XXXXXXX '; $code = rand ( 1000, 9999 ); $SMS = new SMS (); $status = $SMS ->send_verify($mobile, $code); if (! $status) { echo $sms->error; }Copy the code

I hope the above content can help you. Many PHPer will encounter some problems and bottlenecks when they are advanced, and they have no sense of direction when writing too many business codes. I have sorted out some information, including but not limited to: Distributed architecture, high scalability, high performance, high concurrency, server performance tuning, TP6, Laravel, Redis, Swoft, Kafka, Mysql optimization, shell scripting, Docker, microservices, Nginx, etc. Many knowledge points can be free to share with you