Depending on SMS has been slowly fade out the usual communication tools queue, but always erase its existence, SMS verification code depending never be replaced, in addition to important information on the status of the notification is not desirable of. So understand SMS use is necessary in the development of a ring.
Tencent cloud SMS service is provided free of charge in 100, it is convenient for development and testing.
Text messaging service and establishes a template
https://console.cloud.tencent.com/sms
View SDK
https://cloud.tencent.com/document/product/382/13410
It provides multiple-language SDK
PHP SMS templates streamline
Send to achieve a single message template
1 / * * 2 * @param String $ nationCode country code, such as the 86 Chinese 3 * @param String $ phoneNumber without a country code phone number 4 * @param int $ templid template the above mentioned id 5 * @param Array $ params template parameters list, such as template ... {1} {2} {3} ..., you need to take three arguments . 6 * @param String $ Sign signature, if the fill in the blank string, the system will use the default signing . 7 * @param String $ extend the spreading code, can fill in the blank string 8 parameters @param string $ ext returned from the server as *, can fill in the blank string . 9 * @return string response json string, see detailed protocol document Tencent cloud 10 * / . 11 function sendWithParam ( $ nationCode , $ phoneNumber , $ templid = 0, $ params, $ Sign = "", $ Extend = "", $ EXT = "" ) { 12 is 13 is $ AppID = 1400xxx; // own message AppID 14 $ AppKey = "d80axxxxx"; // own message AppKey 15 16 $ random = RAND (100000, 999999); // generate random numbers 17 $ curTime = Time (); 18 $ wholeUrl = "https://yun.tim.qq.com/v5/tlssmssvr/sendsms" "sdkappid =.? . " $ AppID ." & Random =. " $ Random ; 19 20 //Post body tissue according to the protocol package 21 is $ Data = new new \ the stdClass (); // Create a member of methods and properties does not empty object 22 is $ Tel = new new \ the stdClass (); 23 is $ Tel . -> nationcode = "" $ nationCode ; 24 $ Tel -> Mobile = "." $ phoneNumber ; 25 $ Data -> Tel = $ Tel ; 26 is $ Data -> SIG = the hash ( "SHA256", "AppKey =." $ AppKey . "& Random =." Random $ .. "Time = &" $ curTime "Mobile & =.". $ phoneNumber );// generate the signature 27 $ the Data->tpl_id = $templId; 28 $data->params = $params; 29 $data->sign = $sign; 30 $data->time = $curTime; 31 $data->extend = $extend; 32 $data->ext = $ext; 33 34 return sendCurlPost($wholeUrl, $data); 35 } 36 /** 37 * 发送请求 38 * 39 * @Param string $ url request address 40 * @param Array $ dataobj content request 41 is * @return String response json string 42 is * / 43 is function sendCurlPost ( $ URL , $ dataobj ) { 44 is $ curl = curl_init (); 45 curl_setopt ( $ curl , CURLOPT_URL to, $ URL ); 46 is curl_setopt ( $ curl , CURLOPT_HEADER, 0 ); 47 curl_setopt ( $ curl , CURLOPT_RETURNTRANSFER,. 1 ); 48 curl_setopt ( $ curl , CURLOPT_POST,. 1); 49 curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 60); 50 curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($dataObj)); 51 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); 52 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); 53 $ret = curl_exec($curl); 54 if (false == $ret) { 55 // curl_exec failed 56 $result 65 } 66 curl_close($curl); 67 68 return $result; 69 }
Test code:
. 1 function XX () { 2 $ templid = 286xxx; // own message template ID . 3 $ phoneNumber1 = "159xxxxx"; // Accept SMS mobile phone number . 4 the try { . 5 // template placeholder data . 6 $ the params = Array ( "Data 1 "," data 2 " ); . 7 $ Result = sendWithParam (" 86 ", $ phoneNumber1 , $ templid , $ the params ," "," "," " ); . 8 echo $ Result ;// output successful results json 9 } the catch (\ Exception E $ ) { 10 echo var_dump ( $ E ); // output abnormality information 11 } 12 }