<?php
header('content-type:text/html;charset=utf-8');
class
SendCode
{
private
$key
;
private
$tpl_id
;
public
function
__construct()
{
$this
->key = 'AppKey';
$this
->tpl_id = 'tpl_id';
}
public
function
send(
$mobile
){
if
(
empty
(
$mobile
)) {
$this
->show_json(-4,'手机号不能为空');
}
$code
= mt_rand(100000,999999);
$sendUrl
= 'http:
$smsConf
=
array
(
'key' =>
$this
->key,
'mobile' =>
$mobile
,
'tpl_id' =>
$this
->tpl_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){
$data
['code'] =
$code
;
$this
->show_json(1,
$data
);
}
else
{
$msg
=
$result
['reason'];
$this
->show_json(-3,
"短信发送失败("
.
$error_code
.
"):"
.
$msg
);
}
}
else
{
$this
->show_json(-3, '请求发送短信失败');
}
}
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
show_json(
$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
));
}
}