以下为laravel版本. tp版本一样可用只需修改redis以及正确调用我解析后的文件即可
首先需要引入极光提供的SDK文件
在项目中的 composer.json 文件中添加 JSMS 依赖
"require": {
"jiguang/jsms": "~1.0"
}
执行
$ php composer.phar install
或
$ composer install
进行安装。
我把jsms/src下的JSMS.php进行了解释
在app下创建Libraries 然后创建一个JSMS.php文件 复制以下代码
appKey = $appKey;
$this->masterSecret = $masterSecret;
$this->options = array_merge([
'ssl_verify' => true,
'disable_ssl' => false
], $options);
}
//发送验证码
public function sendCode($mobile, $temp_id) {
$url = self::URL . 'codes';
$body = array('mobile' => $mobile, 'temp_id' => $temp_id);
return $this->request('POST', $url, $body);
}
//发送验证码
public function sendVoiceCode($mobile, $options = []) {
$url = self::URL . 'voice_codes';
$body = array('mobile' => $mobile);
if (!empty($options)) {
if (is_array($options)) {
$body = array_merge($options, $body);
} else {
$body['ttl'] = $options;
}
}
return $this->request('POST', $url, $body);
}
//验证的验证码
public function checkCode($msg_id, $code) {
$url = self::URL . 'codes/' . $msg_id . "/valid";
$body = array('code' => $code);
return $this->request('POST', $url, $body);
}
public function sendMessage($mobile, $temp_id, array $temp_para = [], $time = null) {
$path = 'messages';
$body = array(
'mobile' => $mobile,
'temp_id' => $temp_id,
);
if (!empty($temp_para)) {
$body['temp_para'] = $temp_para;
}
if (isset($time)) {
$path = 'schedule';
$body['send_time'] = $time;
}
$url = self::URL . $path;
return $this->request('POST', $url, $body);
}
public function sendBatchMessage($temp_id, array $recipients, $time = null) {
$path = 'messages';
foreach ($recipients as $mobile => $temp_para) {
$r[] = array(
'mobile' => $mobile,
'temp_para' => $temp_para
);
}
$body = array(
'temp_id' => $temp_id,
'recipients' => $r
);
if (isset($time)) {
$path = 'schedule';
$body['send_time'] = $time;
}
$url = self::URL . $path . '/batch';
return $this->request('POST', $url, $body);
}
public function showSchedule($scheduleId) {
$url = self::URL . 'schedule/' . $scheduleId;
return $this->request('GET', $url);
}
public function deleteSchedule($scheduleId) {
$url = self::URL . 'schedule/' . $scheduleId;
return $this->request('DELETE', $url);
}
public function getAppBalance() {
$url = self::URL . 'accounts/app';
return $this->request('GET', $url);
}
private function request($method, $url, $body = []) {
$ch = curl_init();
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true,
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'Connection: Keep-Alive'
),
CURLOPT_USERAGENT => 'JSMS-API-PHP-CLIENT',
CURLOPT_CONNECTTIMEOUT => 20,
CURLOPT_TIMEOUT => 120,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_USERPWD => $this->appKey . ":" . $this->masterSecret,
CURLOPT_URL => $url,
CURLOPT_CUSTOMREQUEST => $method,
);
if (!$this->options['ssl_verify']
|| (bool) $this->options['disable_ssl']) {
$options[CURLOPT_SSL_VERIFYPEER] = false;
$options[CURLOPT_SSL_VERIFYHOST] = 0;
}
if (!empty($body)) {
$options[CURLOPT_POSTFIELDS] = json_encode($body);
}
curl_setopt_array($ch, $options);
$output = curl_exec($ch);
if($output === false) {
return "Error Code:" . curl_errno($ch) . ", Error Message:".curl_error($ch);
} else {
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header_text = substr($output, 0, $header_size);
$body = substr($output, $header_size);
$headers = array();
foreach (explode("\r\n", $header_text) as $i => $line) {
if (!empty($line)) {
if ($i === 0) {
$headers[0] = $line;
} else if (strpos($line, ": ")) {
list ($key, $value) = explode(': ', $line);
$headers[$key] = $value;
}
}
}
$response['headers'] = $headers;
$response['body'] = json_decode($body, true);
$response['http_code'] = $httpCode;
}
curl_close($ch);
return $response;
}
}
引入自己的类库或者函数
在项目目录下composer.json中加入
"autoload": {
"files":[
"app/Libraries/JSMS.php"
]
}
在项目目录下执行
composer dump-autoload
$data,'msg' => $msg,'code' => $code];
}
public function ok($data = [],$msg='成功',$code=200){
return [ 'data' => $data,'msg' => $msg,'code' => $code ];
}
public function index(Request $request){
//如果需要redis进行ip存储,请确保可以连接上redis
$redis=new Redis();
$redis->connect('127.0.0.1',6379);
$key = $this->get_real_ip();//获取客户端真实ip地址
$check = $redis->exists($key);//判断是否存在
if($check){//如果存在
$redis->incr($key);//值自增1
$count = $redis->get($key);//获取值
if($count > 3){
return $this->no('','获取频率太快,请稍后尝试');
}
}else{
//限制时间为300秒
$redis->set($key,0,300);//设置reids键为k,值为0,有效时间300秒
}
//-------------------短信开始-------------------//
//1.初始化
$app_key = 'XXXXXXXXXXXXXXXXXXX';
$master_secret = 'XXXXXXXXXXXXXX';
$temp_id = '1';//设置的短信模板id
// $temp_para = ['test' => 'jiguang'];
$client = new JSMS($app_key, $master_secret);
//-------------------短信结束-------------------//
$data = $request->all();
$phone = $data['phone'];
//$msg_id: 发送验证码 sendCode 函数返回的数组中的 msg_id 键对应的值
//2.证书问题
$client = new JSMS($app_key, $master_secret, [ 'disable_ssl' => true ]);
//3.发送短信验证码
$data = $client->sendCode($phone, $temp_id);
$msg_id = $data['body']['msg_id'];//获取到当前手机号码验证码的唯一id
return $this->ok($msg_id);
}
function check($msg_id,$code){
$app_key = 'XXXXXXXX';
$master_secret = 'XXXXXXXXXXXXXXX';
$client = new JSMS($app_key, $master_secret, [ 'disable_ssl' => true ]);
$check = $client->checkCode($msg_id, $code);//用id和验证码去服务器进行校验
$is_ok=$check['http_code'];
return $is_ok;//返回状态码
}
//获取客户端真实ip地址
function get_real_ip(){
static $realip;
if(isset($_SERVER)){
if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])){
$realip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}else if(isset($_SERVER['HTTP_CLIENT_IP'])){
$realip=$_SERVER['HTTP_CLIENT_IP'];
}else{
$realip=$_SERVER['REMOTE_ADDR'];
}
}else{
if(getenv('HTTP_X_FORWARDED_FOR')){
$realip=getenv('HTTP_X_FORWARDED_FOR');
}else if(getenv('HTTP_CLIENT_IP')){
$realip=getenv('HTTP_CLIENT_IP');
}else{
$realip=getenv('REMOTE_ADDR');
}
}
return $realip;
}
}
前端利用ajax请求发送验证码获取到msg_id, 提交表单时请求 check() 方法传入msg_id以及验证码即可进行校对
第一次写文章. 如果有错,请各位大佬多多包涵