<?php
class
Sms{
private
$_api_key
= '';
private
$_last_error
=
array
();
private
$_use_ssl
= FALSE;
private
$_ssl_api_url
=
array
(
'send' => 'https:
'send_batch' => 'https:
'status' => 'https:
);
private
$_api_url
=
array
(
'send' => 'http:
'send_batch' => 'http:
'status' => 'http:
);
public
function
__construct(
$param
=
array
() ){
if
( !isset(
$param
['api_key'] ) ){
die
(
"api key error."
);
}
if
( isset(
$param
['api_key'] ) ){
$this
->_api_key =
$param
['api_key'];
}
if
( isset(
$param
['use_ssl'] ) ){
$this
->_use_ssl =
$param
['use_ssl'];
}
}
public
function
send(
$mobile
,
$message
= '' ){
$api_url
= !
$this
->_use_ssl ?
$this
->_api_url['send'] :
$this
->_ssl_api_url['send'];
$param
=
array
(
'mobile' =>
$mobile
,
'message' =>
$message
,
);
$res
=
$this
->http_post(
$api_url
,
$param
);
return
@json_decode(
$res
,TRUE );
}
public
function
send_batch(
$mobile_list
=
array
() ,
$message
=
array
() ,
$time
= '' ){
$api_url
= !
$this
->_use_ssl ?
$this
->_api_url['send_batch'] :
$this
->_ssl_api_url['send_batch'];
$mobile_list
=
is_array
(
$mobile_list
) ? implode( ',' ,
$mobile_list
) :
$mobile_list
;
$param
=
array
(
'mobile_list' =>
$mobile_list
,
'message' =>
$message
,
'time' =>
$time
,
);
$res
=
$this
->http_post(
$api_url
,
$param
);
return
@json_decode(
$res
,TRUE );
}
public
function
get_deposit(){
$api_url
= !
$this
->_use_ssl ?
$this
->_api_url['status'] :
$this
->_ssl_api_url['status'];
$res
=
$this
->http_get(
$api_url
);
return
@json_decode(
$res
,TRUE );
}
public
function
recv(
$type
= 'status' ,
$param
=
array
() ){
if
(
$type
== 'status' ){
if
(
$param
['batch_id'] &&
$param
['mobile'] &&
$param
['status'] ){
}
}
elseif
(
$type
== 'incoming' ){
if
(
$param
['mobile'] &&
$param
['message'] ){
}
}
}
private
function
http_post(
$api_url
= '' ,
$param
=
array
() ,
$timeout
= 5 ){
if
( !
$api_url
){
die
(
"error api_url"
);
}
$ch
= curl_init();
curl_setopt(
$ch
, CURLOPT_URL,
$api_url
);
curl_setopt(
$ch
, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_0 );
curl_setopt(
$ch
, CURLOPT_CONNECTTIMEOUT,
$timeout
);
curl_setopt(
$ch
, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt(
$ch
, CURLOPT_HEADER, FALSE);
if
(
parse_url
(
$api_url
)['scheme'] == 'https' ){
curl_setopt(
$ch
, CURLOPT_SSL_VERIFYHOST , FALSE);
curl_setopt(
$ch
, CURLOPT_SSL_VERIFYPEER , FALSE);
}
curl_setopt(
$ch
, CURLOPT_HTTPAUTH , CURLAUTH_BASIC);
curl_setopt(
$ch
, CURLOPT_USERPWD , 'api:key-'.
$this
->_api_key );
curl_setopt(
$ch
, CURLOPT_POST, TRUE);
curl_setopt(
$ch
, CURLOPT_POSTFIELDS,
$param
);
$res
= curl_exec(
$ch
);
$error
= curl_error(
$ch
);
curl_close(
$ch
);
if
(
$error
){
$this
->_last_error[] =
$error
;
return
FALSE;
}
return
$res
;
}
private
function
http_get(
$api_url
= '' ,
$timeout
= '' ){
if
( !
$api_url
){
die
(
"error api_url"
);
}
$ch
= curl_init();
curl_setopt(
$ch
, CURLOPT_URL,
$api_url
);
curl_setopt(
$ch
, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_0 );
curl_setopt(
$ch
, CURLOPT_CONNECTTIMEOUT,
$timeout
);
curl_setopt(
$ch
, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt(
$ch
, CURLOPT_HEADER, FALSE);
if
(
parse_url
(
$api_url
)['scheme'] == 'https' ){
curl_setopt(
$ch
, CURLOPT_SSL_VERIFYHOST , FALSE);
curl_setopt(
$ch
, CURLOPT_SSL_VERIFYPEER , FALSE);
}
curl_setopt(
$ch
, CURLOPT_HTTPAUTH , CURLAUTH_BASIC);
curl_setopt(
$ch
, CURLOPT_USERPWD , 'api:key-'.
$this
->_api_key );
$res
= curl_exec(
$ch
);
$error
= curl_error(
$ch
);
curl_close(
$ch
);
if
(
$error
){
$this
->_last_error[] = curl_error(
$ch
);
return
FALSE;
}
return
$res
;
}
public
function
last_error(){
return
$this
->_last_error;
}
}