萬商超信
發(fā)送短信驗(yàn)證碼平臺(thinkphp短信驗(yàn)證碼接口)
2021-11-17 21:03
后端PHP代碼:
/** * 發(fā)送短信驗(yàn)證碼 * @return hink esponseJson * @author 申霖 * @time 2019/7/6 0006 上午 10:21 */ public function send() { $phone = input('post.phone'); $result = $this->validate(['phone' => $phone], 'appcommonalidateMember.send'); if (true !== $result) { // 驗(yàn)證失敗 輸出錯誤信息 return json(['code' => 101, 'msg' => $result]); } $code = rand(100000, 999999); header('content-type:text/html;charset=utf-8'); $sendUrl = 'http://v.juhe.cn/sms/send'; //短信接口的URL $smsConf = [ 'key' => "3fe714**********7068d04a9e4dfa7", //您申請的APPKEY 'mobile' => $phone, //接受短信的用戶手機(jī)號碼 'tpl_id' => "60904", //您申請的短信模板ID,根據(jù)實(shí)際情況修改 'tpl_value' => "#code#=$codecompany#=聚合數(shù)據(jù)" //您設(shè)置的模板變量,根據(jù)實(shí)際情況修改 ]; $content = $this->juhecurl($sendUrl, $smsConf, 1); //請求發(fā)送短信 if ($content) { $result = json_decode($content, true); $error_code = $result['error_code']; if ($error_code == 0) { session('code' . $phone, $code, 'indexBlog'); //狀態(tài)為0,說明短信發(fā)送成功 return json(['code' => 200, 'msg' => '發(fā)送成功']); } else { //狀態(tài)非0,說明失敗 $msg = $result['reason']; return json(['code' => 100, 'msg' => "短信發(fā)送失敗(" . $error_code . "):" . $msg]); } } else { //返回內(nèi)容異常,以下可根據(jù)業(yè)務(wù)邏輯自行修改 return json(['code' => 103, 'msg' => "請求發(fā)送短信失敗"]); } } /** * curl * @param $url * @param bool $params * @param int $isPost * @return bool|mixed */ public function juhecurl($url, $params = false, $isPost = 0) { // $httpInfo = []; $ch = curl_init(); curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22'); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); curl_setopt($ch, CURLOPT_TIMEOUT, 30); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); if ($isPost) { curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $params); curl_setopt($ch, CURLOPT_URL, $url); } else { if ($params) { curl_setopt($ch, CURLOPT_URL, $url . '?' . $params); } else { curl_setopt($ch, CURLOPT_URL, $url); } } $response = curl_exec($ch); if ($response === FALSE) { //echo "cURL Error: " . curl_error($ch); return false; } // $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); // $httpInfo = array_merge($httpInfo, curl_getinfo($ch)); curl_close($ch); return $response; }
前端調(diào)取接口,手機(jī)號為參數(shù)。[發(fā)送短信驗(yàn)證碼平臺(thinkphp短信驗(yàn)證碼接口)]。