本篇内容主要讲解“PHP实现发送短信验证码的方法是什么”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“PHP实现发送短信验证码的方法是什么”吧!
创建好HTML和js文件。
创建“Msm.php”文件,内容为“public function sendmsm(){...}”。
通过CURL发送HTTP请求即可。
实例
public function sendmsm(){$phone = input('phone');if(!$phone){return WPreturn('请输入手机号码!',-1);} $code = rand(1000,9999); $_SESSION['code'] = $code; $res = sendmessage($code ,$phone); if($res){ return WPreturn('发送成功',1); }else{ return WPreturn('发送验证码失败!',-1); } } public function sendmessage($code, $telephone) { $conf = getconf(''); if(!$code){ return false; } if(!$telephone){ return false; } $content = "您的验证码是:{$code},如非本人操作,请忽略此短信。"; //创蓝接口参数 $postArr = array ( 'account' => $conf['msm_appkey'], 'password' => $conf['msm_secretkey'], 'msg' => urlencode($content), 'phone' => $telephone, 'report' => true ); $result = $this->curlPost("http://smssh2.253.com/msg/send/json", $postArr); $json = json_decode($result); if($json -> code != 0){ return false; }else{ return true; } } private function curlPost($url,$postFields){ $postFields = json_encode($postFields); $ch = curl_init (); curl_setopt( $ch, CURLOPT_URL, $url ); curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json; charset=utf-8' //json版本需要填写 Content-Type: application/json; ) ); curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt( $ch, CURLOPT_POST, 1 ); curl_setopt( $ch, CURLOPT_POSTFIELDS, $postFields); curl_setopt( $ch, CURLOPT_TIMEOUT,60); curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0); $ret = curl_exec ( $ch ); if (false == $ret) { $result = curl_error( $ch); } else { $rsp = curl_getinfo( $ch, CURLINFO_HTTP_CODE); if (200 != $rsp) { $result = "请求状态 ". $rsp . " " . curl_error($ch); } else { $result = $ret; } } curl_close ( $ch ); return $result; }
到此,相信大家对“PHP实现发送短信验证码的方法是什么”有了更深的了解,不妨来实际操作一番吧!这里是编程网网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!