Home>Article>Backend Development> How to send SMS notification in php
How to send SMS notifications in php
It is very common to send SMS verification codes when building a website, such as a mobile phone number Registration, password retrieval, etc. Let’s learn how to achieve it.
1. The front-end and back-end interaction process of mobile phone number registration;
(1) The user operates the client and clicks to send the verification code, (then the client sends an ajax request, And the button is in 60s silent time)
(2) The back-end logic layer accepts the passed mobile phone number, verifies whether the format is legal, and enters the database to see if the match exists
(3) Generate verification code , stored in the session, and then curl is sent to the third-party SMS interface link
(4) Configure the third-party SMS platform to send verification code information to the specified mobile phone number user
2 . Today we mainly explain the configuration of the third-party platform and the logic of background processing;
3. The selected third-party platformis: (You can also choose other platforms, among which The principles are almost similar)
(1) Saidi Cloud Communication (https://www.mysubmail.com)
(2) Register, real-name authentication, enter products and services, create new Template, its template syntax can be viewed in its supporting documentation
(3) Go to the "Create/Manage AppIdD" column, create and enable the project, and then generate the default "APPID" And "APPKEY"
(4) Go to the homepage, click on the "View API Development Documents" link, and get the link to the POST request:
https://api.mysubmail.com/message/xsend
4. Backend logic development:
Core: Create verification code -> Encapsulate POST request function -> Send verification code to third-party platform -> Then forward it to the user by the third party Mobile phone
(1) Send verification code to the third-party platform:
private function sendMessage() { $appId = "xxxxx"; $appKey = "36426a9xxxxxxxxxxxx7bed8583a3c"; $code = $this->makeCode(6); $data = [ "appid" => $appId, "to" => $this->phone, "project" => "FoJ494", "vars" => '{"code":' . $code . ',"time":"60"}', "signature" => $appKey, ]; $res = $this->httpRequest($data); var_dump($res); }
(2) Request link to send verification code data to the third-party platform
private function httpRequest($data) { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $this->RequestUrl); curl_setopt($curl, CURLOPT_HEADER, 0); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); if (isset($data)) { curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); } $res = curl_exec($curl); var_dump(curl_error($curl)); //打印请求错误 curl_close($curl); return $res; }
5. Final effect
For more PHP related knowledge, please visitPHP Chinese website!
The above is the detailed content of How to send SMS notification in php. For more information, please follow other related articles on the PHP Chinese website!