万商超信
发送短信验证码平台(thinkphp短信验证码接口)
2021-11-17 21:03
后端PHP代码:
/** * 发送短信验证码 * @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) { // 验证失败 输出错误信息 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, //接受短信的用户手机号码 'tpl_id' => "60904", //您申请的短信模板ID,根据实际情况修改 'tpl_value' => "#code#=$codecompany#=聚合数据" //您设置的模板变量,根据实际情况修改 ]; $content = $this->juhecurl($sendUrl, $smsConf, 1); //请求发送短信 if ($content) { $result = json_decode($content, true); $error_code = $result['error_code']; if ($error_code == 0) { session('code' . $phone, $code, 'indexBlog'); //状态为0,说明短信发送成功 return json(['code' => 200, 'msg' => '发送成功']); } else { //状态非0,说明失败 $msg = $result['reason']; return json(['code' => 100, 'msg' => "短信发送失败(" . $error_code . "):" . $msg]); } } else { //返回内容异常,以下可根据业务逻辑自行修改 return json(['code' => 103, 'msg' => "请求发送短信失败"]); } } /** * 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; }
前端调取接口,手机号为参数。[发送短信验证码平台(thinkphp短信验证码接口)]。