| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- <?php
- class Sms
- {
- private $api_url, $auth_key, $errorMsg;
- function __construct($api_url, $auth_key)
- {
- $this->api_url = $api_url;
- $this->auth_key = $auth_key;
- }
- /**
- * 实现短信验证码接口
- *
- */
- public function sendSms($mobile, $code)
- {
- $send = array(
- // 'apikey' => $this->auth_key,
- 'username' => Doo::conf()->SMS_USER,
- 'password' => Doo::conf()->SMS_PWD,
- 'mobile' => $mobile,
- // 'text' => $code
- 'content' => $code
- );
- $data = http_build_query($send);
- // $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["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)
- {
- if (is_object($array)) {
- $array = (array)$array;
- }
- if (is_array($array)) {
- foreach ($array as $key => $value) {
- $array[$key] = $this->objectToArray($value);
- }
- }
- return $array;
- }
- /**
- * POST方式访问短信接口
- * @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 {
- $ch = curl_init();
- 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_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;
- } catch (Exception $e) {
- $this->errorMsg = $e->getMessage();
- return false;
- }
- }
- /**
- * POST方式访问短信接口
- * @param string $data
- * @return mixed
- */
- public function getErrors()
- {
- return $this->errorMsg;
- }
- }
|