|
@@ -2,14 +2,12 @@
|
|
|
|
|
|
class Sms
|
|
|
{
|
|
|
- private $api_url, $account, $password, $extno, $errorMsg;
|
|
|
+ private $api_url, $auth_key, $errorMsg;
|
|
|
|
|
|
- function __construct($api_url, $account,$password,$extno)
|
|
|
+ function __construct($api_url, $auth_key)
|
|
|
{
|
|
|
$this->api_url = $api_url;
|
|
|
- $this->account = $account;
|
|
|
- $this->password = $password;
|
|
|
- $this->extno = $extno;
|
|
|
+ $this->auth_key = $auth_key;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -19,38 +17,20 @@ class Sms
|
|
|
public function sendSms($mobile, $code)
|
|
|
{
|
|
|
$send = array(
|
|
|
- 'action' => 'send',
|
|
|
- 'account' => $this->account,
|
|
|
- 'password' => $this->password,
|
|
|
+ 'apikey' => $this->auth_key,
|
|
|
'mobile' => $mobile,
|
|
|
- 'content' => $code,
|
|
|
- 'extno' => $this->extno
|
|
|
+ 'text' => $code
|
|
|
);
|
|
|
$data = http_build_query($send);
|
|
|
- $res = simplexml_load_string($this->_httpClient($this->api_url,$data));
|
|
|
+ $res = json_decode($this->_httpClient($this->api_url,$data));
|
|
|
$resArr = $this->objectToArray($res);
|
|
|
- $resultcode = !empty($resArr) ? explode('#@#',$resArr['resplist']['resp'])[2] : '';
|
|
|
- if ($resultcode == '0') return true;
|
|
|
+ if (!empty($resArr) && $resArr["code"] == 0) return true;
|
|
|
else {
|
|
|
- if (empty($this->errorMsg)) $this->errorMsg = !empty($resultcode) ? $this->getErrorMsg($resultcode) : '未知错误';
|
|
|
+ if (empty($this->errorMsg)) $this->errorMsg = !empty($resArr["msg"]) ? $resArr["msg"] : '未知错误';
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- function getErrorMsg($code){
|
|
|
- $msg = '';
|
|
|
- switch($code){
|
|
|
- case 6: $msg = '错误代码:6,请联系我们客服';break;
|
|
|
- case 10: $msg = '错误代码:10,请联系我们客服';break;
|
|
|
- case 12: $msg = '检查接收短信手机号码格式是否正确';break;
|
|
|
- case 15: $msg = '错误代码:15,请联系我们客服';break;
|
|
|
- case 16: $msg = '一天时间内同一个手机号码不能太频繁接收短信';break;
|
|
|
- case 17: $msg = '错误代码:17,请联系我们客服';break;
|
|
|
- default : $msg = '错误代码:400,请联系我们客服';break;
|
|
|
- }
|
|
|
- return $msg;
|
|
|
- }
|
|
|
-
|
|
|
//对象转数组,使用get_object_vars返回对象属性组成的数组
|
|
|
function objectToArray($array)
|
|
|
{
|
|
@@ -74,10 +54,13 @@ class Sms
|
|
|
{
|
|
|
try {
|
|
|
$ch = curl_init();
|
|
|
- curl_setopt($ch, CURLOPT_URL,$api_url.$data);
|
|
|
+ curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:text/plain;charset=utf-8', 'Content-Type:application/x-www-form-urlencoded', 'charset=utf-8'));
|
|
|
+ curl_setopt($ch, CURLOPT_URL, $api_url);
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
- curl_setopt($ch, CURLOPT_HEADER, 0);
|
|
|
+ curl_setopt($ch, CURLOPT_POST, 1);
|
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
|
|
|
+ curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
|
|
+ curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
|
|
|
$res = curl_exec($ch);
|
|
|
curl_close($ch);
|
|
|
return $res;
|