ソースを参照

短信模板更新

laiguoran 5 年 前
コミット
e101ccb23b
2 ファイル変更62 行追加9 行削除
  1. 47 6
      protected/class/sms.php
  2. 15 3
      protected/config/sms.conf.php

+ 47 - 6
protected/class/sms.php

@@ -17,20 +17,42 @@ class Sms
     public function sendSms($mobile, $code)
     {
         $send = array(
-            'apikey' => $this->auth_key,
+//            'apikey' => $this->auth_key,
+            'username' => Doo::conf()->SMS_USER,
+            'password' => Doo::conf()->SMS_PWD,
             'mobile' => $mobile,
-            'text' => $code
+//            'text' => $code
+            'content' => $code
         );
         $data = http_build_query($send);
-        $res = json_decode($this->_httpClient($this->api_url,$data));
-        $resArr = $this->objectToArray($res);
-        if (!empty($resArr) && $resArr["code"] == 0) return true;
+//        $res = $this->_httpClient(Doo::conf()->SMS_URL, $data);
+        $res = $this->_httpGet(Doo::conf()->SMS_URL.'?'.$data);
+//        $resArr = $this->objectToArray($res);
+        parse_str($res, $resArr);
+        if (!empty($resArr) && $resArr["result"] == 0) return true;
         else {
-            if (empty($this->errorMsg)) $this->errorMsg = !empty($resArr["msg"]) ? $resArr["msg"] : '未知错误';
+            if (empty($this->errorMsg)) $this->errorMsg = !empty($resArr["result"]) ? $this->errorMsg($resArr["result"]) : '未知错误';
             return false;
         }
     }
 
+    function errorMsg($status) {
+        $msg = '';
+        switch ($status) {
+            case -100: $msg = '参数有误';break;
+            case -101: $msg = '帐号和密码验证失败或是帐号被注销';break;
+            case -102: $msg = '手机号码为空或含有不合法的手机号码';break;
+            case -103: $msg = '内容为空或含有非法字符';break;
+            case -105: $msg = '扩展码错误';break;
+            case -107: $msg = '查询频繁,超过查询频率';break;
+            case -108: $msg = '查询状态无量';break;
+            case -109: $msg = 'IP验证错误';break;
+            case -110: $msg = '其他错误';break;
+            case -111: $msg = '定时时间格式错误';break;
+        }
+        return $msg;
+    }
+
     //对象转数组,使用get_object_vars返回对象属性组成的数组
     function objectToArray($array)
     {
@@ -50,6 +72,25 @@ class Sms
      * @param string $data
      * @return mixed
      */
+
+    function _httpGet($url=""){
+
+        $curl = curl_init();
+
+        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
+        curl_setopt($curl, CURLOPT_TIMEOUT, 500);
+        // 为保证第三方服务器与微信服务器之间数据传输的安全性,所有微信接口采用https方式调用,必须使用下面2行代码打开ssl安全校验。
+        // 如果在部署过程中代码在此处验证失败,请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。
+        // curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
+        // curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
+        curl_setopt($curl, CURLOPT_URL, $url);
+
+        $res = curl_exec($curl);
+        curl_close($curl);
+
+        return $res;
+    }
+
     private function _httpClient($api_url, $data)
     {
         try {

ファイルの差分が大きいため隠しています
+ 15 - 3
protected/config/sms.conf.php