Sms.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2015/12/24
  6. * Time: 16:56
  7. */
  8. namespace common\library;
  9. use common\models\Shortmsg;
  10. /**
  11. * 短信接口
  12. * Class Sms
  13. * @package common\libary
  14. */
  15. class Sms {
  16. const LIMIT_SECOND = 60; //同个号码发送频率
  17. const TIMEOUT = 30; //超时
  18. const URL="http://www.ztsms.cn/sendNSms.do";
  19. public $data;
  20. const USERNAME = 'wanzhw';//用户名
  21. const PASSWORD = 'WANzhw2017';//原密码
  22. const PRODUCTID = 676767;//产品id
  23. /**
  24. * 新短信接口接入
  25. * @param $mobiles
  26. * @param $content
  27. * @param bool $limit
  28. * @return bool
  29. */
  30. public function SendMessage($mobiles,$content, $limit= true){
  31. if($limit && !self::limitTime($mobiles)){
  32. return false;
  33. }
  34. if(empty($mobiles) && !self::mobileFormat($mobiles)){
  35. return false;
  36. }
  37. $this->data = array(
  38. 'content' => $content,//短信内容
  39. 'mobile' => $mobiles,//手机号码
  40. 'productid' => self::PRODUCTID,//产品id
  41. );
  42. //$cust_code = \Yii::$app->params['message']['id'];
  43. //$password =\Yii::$app->params['message']['pwd'];
  44. $result = self::sendSMS("POST");
  45. $resultarr = self::resultHandle($result);
  46. self::saveShortMessage($mobiles,$content,$resultarr);
  47. return $resultarr;
  48. }
  49. static function saveShortMessage($mobiles,$content,$resultarr){
  50. if($resultarr==false)
  51. return false;
  52. $short = new Shortmsg();
  53. $short->msgid = isset($resultarr[1])?$resultarr[1]:"";
  54. $short->recipient = $mobiles;
  55. $short->content = $content;
  56. $short->receipt = isset($resultarr[1])?'0':"失败";
  57. $short->created_at = time();
  58. $short->sendtime = time();
  59. if($short->validate() && $short->save()){
  60. }
  61. }
  62. /**
  63. * 限制请求频率
  64. * @param $mobile
  65. * @return bool
  66. */
  67. static function limitTime($mobile){
  68. $shortmsg = Shortmsg::find()->where('recipient = :mobile',[':mobile'=>$mobile])->orderBy('created_at DESC')->one();
  69. if(empty($shortmsg)){
  70. return true;
  71. }else{
  72. if($shortmsg->created_at < (time()-self::LIMIT_SECOND)){
  73. return true;
  74. }
  75. }
  76. return false;
  77. }
  78. /**
  79. * 验证手机格式
  80. * @param $mobile
  81. * @return int
  82. */
  83. static function mobileFormat($mobile){
  84. return preg_match('/^1[34578]\d{9}$/',$mobile);
  85. }
  86. /**
  87. * 返回数据处理
  88. * @return bool
  89. */
  90. static function resultHandle($result){
  91. if(!empty($result))
  92. {
  93. $resultarr = explode(',',$result);
  94. }else{
  95. return false;
  96. }
  97. if($resultarr[0]!=1)
  98. {
  99. return false;
  100. }
  101. return $resultarr;
  102. }
  103. private function httpPost(){ // 模拟提交数据函数
  104. $curl = curl_init(); // 启动一个CURL会话
  105. curl_setopt($curl, CURLOPT_URL,self::URL); // 要访问的地址
  106. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
  107. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
  108. curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
  109. curl_setopt($curl, CURLOPT_POST, true); // 发送一个常规的Post请求
  110. curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($this->data)); // Post提交的数据包
  111. curl_setopt($curl, CURLOPT_TIMEOUT, self::TIMEOUT); // 设置超时限制防止死循环
  112. curl_setopt($curl, CURLOPT_HEADER, false); // 显示返回的Header区域内容
  113. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // 获取的信息以文件流的形式返回
  114. $result = curl_exec($curl); // 执行操作
  115. if (curl_errno($curl)) {
  116. echo 'Error POST'.curl_error($curl);
  117. }
  118. curl_close($curl); // 关键CURL会话
  119. return $result; // 返回数据
  120. }
  121. /**
  122. * @param $type|提交类型 POST/GET
  123. * @param $isTranscoding|是否需要转 $isTranscoding 是否需要转utf-8 默认 false
  124. * @return mixed
  125. */
  126. public function sendSMS($type, $isTranscoding = false) {
  127. $this->data['content'] = $isTranscoding === true ? mb_convert_encoding($this->data['content'], "UTF-8") : $this->data['content'];
  128. $this->data['username'] = self::USERNAME;
  129. $this->data['tkey'] = date('YmdHis');
  130. $this->data['password'] = md5(md5(self::PASSWORD) . $this->data['tkey']);
  131. return $type == "POST" ? $this->httpPost() : $this->httpGet();
  132. }
  133. }