sms.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. class Sms
  3. {
  4. private $api_url, $auth_key, $errorMsg;
  5. function __construct($api_url, $auth_key)
  6. {
  7. $this->api_url = $api_url;
  8. $this->auth_key = $auth_key;
  9. }
  10. /**
  11. * 实现短信验证码接口
  12. *
  13. */
  14. public function sendSms($mobile, $code)
  15. {
  16. $send = array(
  17. // 'apikey' => $this->auth_key,
  18. 'username' => Doo::conf()->SMS_USER,
  19. 'password' => Doo::conf()->SMS_PWD,
  20. 'mobile' => $mobile,
  21. // 'text' => $code
  22. 'content' => $code
  23. );
  24. $data = http_build_query($send);
  25. // $res = $this->_httpClient(Doo::conf()->SMS_URL, $data);
  26. $res = $this->_httpGet(Doo::conf()->SMS_URL.'?'.$data);
  27. // $resArr = $this->objectToArray($res);
  28. parse_str($res, $resArr);
  29. if (!empty($resArr) && $resArr["result"] == 0) return true;
  30. else {
  31. if (empty($this->errorMsg)) $this->errorMsg = !empty($resArr["result"]) ? $this->errorMsg($resArr["result"]) : '未知错误';
  32. return false;
  33. }
  34. }
  35. function errorMsg($status) {
  36. $msg = '';
  37. switch ($status) {
  38. case -100: $msg = '参数有误';break;
  39. case -101: $msg = '帐号和密码验证失败或是帐号被注销';break;
  40. case -102: $msg = '手机号码为空或含有不合法的手机号码';break;
  41. case -103: $msg = '内容为空或含有非法字符';break;
  42. case -105: $msg = '扩展码错误';break;
  43. case -107: $msg = '查询频繁,超过查询频率';break;
  44. case -108: $msg = '查询状态无量';break;
  45. case -109: $msg = 'IP验证错误';break;
  46. case -110: $msg = '其他错误';break;
  47. case -111: $msg = '定时时间格式错误';break;
  48. }
  49. return $msg;
  50. }
  51. //对象转数组,使用get_object_vars返回对象属性组成的数组
  52. function objectToArray($array)
  53. {
  54. if (is_object($array)) {
  55. $array = (array)$array;
  56. }
  57. if (is_array($array)) {
  58. foreach ($array as $key => $value) {
  59. $array[$key] = $this->objectToArray($value);
  60. }
  61. }
  62. return $array;
  63. }
  64. /**
  65. * POST方式访问短信接口
  66. * @param string $data
  67. * @return mixed
  68. */
  69. function _httpGet($url=""){
  70. $curl = curl_init();
  71. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  72. curl_setopt($curl, CURLOPT_TIMEOUT, 500);
  73. // 为保证第三方服务器与微信服务器之间数据传输的安全性,所有微信接口采用https方式调用,必须使用下面2行代码打开ssl安全校验。
  74. // 如果在部署过程中代码在此处验证失败,请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。
  75. // curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  76. // curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  77. curl_setopt($curl, CURLOPT_URL, $url);
  78. $res = curl_exec($curl);
  79. curl_close($curl);
  80. return $res;
  81. }
  82. private function _httpClient($api_url, $data)
  83. {
  84. try {
  85. $ch = curl_init();
  86. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:text/plain;charset=utf-8', 'Content-Type:application/x-www-form-urlencoded', 'charset=utf-8'));
  87. curl_setopt($ch, CURLOPT_URL, $api_url);
  88. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  89. curl_setopt($ch, CURLOPT_POST, 1);
  90. curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  91. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  92. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  93. $res = curl_exec($ch);
  94. curl_close($ch);
  95. return $res;
  96. } catch (Exception $e) {
  97. $this->errorMsg = $e->getMessage();
  98. return false;
  99. }
  100. }
  101. /**
  102. * POST方式访问短信接口
  103. * @param string $data
  104. * @return mixed
  105. */
  106. public function getErrors()
  107. {
  108. return $this->errorMsg;
  109. }
  110. }