Repository navigation
Expand file tree
/
Copy pathSendCodeTools.php
More file actions
119 lines (96 loc) · 3.53 KB
/
Copy pathSendCodeTools.php
File metadata and controls
119 lines (96 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
header('content-type:text/html;charset=utf-8');
/**
* 聚合数据发送手机验证码工具类
* Class SendCode
*/
class SendCode
{
private $key;
private $tpl_id;
public function __construct()
{
$this->key = 'AppKey'; // 聚合数据后台的AppKey
$this->tpl_id = 'tpl_id'; // 申请的短信模板ID,根据实际情况修改短信模板
}
public function send($mobile){
if (empty($mobile)) {
$this->showJson(-4,'手机号不能为空');
}
$code = mt_rand(100000,999999);
$sendUrl = 'http://v.juhe.cn/sms/send'; //短信接口的URL
$smsConf = array(
'key' => $this->key, //您申请的APPKEY
'mobile' => $mobile, //接受短信的用户手机号码
'tpl_id' => $this->tpl_id, //您申请的短信模板ID,根据实际情况修改
'tpl_value' =>'#code#='.$code.'&#company#=聚合数据' //您设置的模板变量,根据实际情况修改
);
$content = $this->juhecurl($sendUrl,$smsConf, 1); //请求发送短信
if($content){
$result = json_decode($content,true);
$error_code = $result['error_code'];
if($error_code == 0){
//状态为0,说明短信发送成功
$data['code'] = $code;
$this->showJson(1, $data);
}else{
//状态非0,说明失败
$msg = $result['reason'];
$this->showJson(-3, "短信发送失败(".$error_code."):".$msg);
}
}else{
//返回内容异常,以下可根据业务逻辑自行修改
$this->showJson(-3, '请求发送短信失败');
}
}
/**
* 请求接口返回内容
* @param string $url [请求的URL地址]
* @param string $params [请求的参数]
* @param int $ipost [是否采用POST形式]
* @return string
*/
public function juhecurl($url,$params=false,$ispost=0){
$httpInfo = array();
$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) {
return false;
}
$httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
$httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
curl_close( $ch );
return $response;
}
public function showJson($status = 1, $return = NULL) {
$ret = array('status' => $status);
if (!is_array($return)) {
if ($return) {
$ret['result']['message'] = $return;
}
exit(json_encode($ret));
} else {
$ret['result'] = $return;
}
exit(json_encode($ret));
}
}