123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Administrator
- * Date: 2015/12/24
- * Time: 16:56
- */
- namespace common\library;
- use common\models\Shortmsg;
- /**
- * 短信接口
- * Class Sms
- * @package common\libary
- */
- class Sms {
- const LIMIT_SECOND = 60; //同个号码发送频率
- const TIMEOUT = 30; //超时
- const URL="http://www.ztsms.cn/sendNSms.do";
- public $data;
- const USERNAME = 'wanzhw';//用户名
- const PASSWORD = 'WANzhw2017';//原密码
- const PRODUCTID = 676767;//产品id
- /**
- * 新短信接口接入
- * @param $mobiles
- * @param $content
- * @param bool $limit
- * @return bool
- */
- public function SendMessage($mobiles,$content, $limit= true){
- if($limit && !self::limitTime($mobiles)){
- return false;
- }
- if(empty($mobiles) && !self::mobileFormat($mobiles)){
- return false;
- }
- $this->data = array(
- 'content' => $content,//短信内容
- 'mobile' => $mobiles,//手机号码
- 'productid' => self::PRODUCTID,//产品id
- );
- //$cust_code = \Yii::$app->params['message']['id'];
- //$password =\Yii::$app->params['message']['pwd'];
- $result = self::sendSMS("POST");
- $resultarr = self::resultHandle($result);
- self::saveShortMessage($mobiles,$content,$resultarr);
- return $resultarr;
- }
- static function saveShortMessage($mobiles,$content,$resultarr){
- if($resultarr==false)
- return false;
- $short = new Shortmsg();
- $short->msgid = isset($resultarr[1])?$resultarr[1]:"";
- $short->recipient = $mobiles;
- $short->content = $content;
- $short->receipt = isset($resultarr[1])?'0':"失败";
- $short->created_at = time();
- $short->sendtime = time();
- if($short->validate() && $short->save()){
- }
- }
- /**
- * 限制请求频率
- * @param $mobile
- * @return bool
- */
- static function limitTime($mobile){
- $shortmsg = Shortmsg::find()->where('recipient = :mobile',[':mobile'=>$mobile])->orderBy('created_at DESC')->one();
- if(empty($shortmsg)){
- return true;
- }else{
- if($shortmsg->created_at < (time()-self::LIMIT_SECOND)){
- return true;
- }
- }
- return false;
- }
- /**
- * 验证手机格式
- * @param $mobile
- * @return int
- */
- static function mobileFormat($mobile){
- return preg_match('/^1[34578]\d{9}$/',$mobile);
- }
- /**
- * 返回数据处理
- * @return bool
- */
- static function resultHandle($result){
- if(!empty($result))
- {
- $resultarr = explode(',',$result);
- }else{
- return false;
- }
- if($resultarr[0]!=1)
- {
- return false;
- }
- return $resultarr;
- }
- private function httpPost(){ // 模拟提交数据函数
- $curl = curl_init(); // 启动一个CURL会话
- curl_setopt($curl, CURLOPT_URL,self::URL); // 要访问的地址
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
- curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
- curl_setopt($curl, CURLOPT_POST, true); // 发送一个常规的Post请求
- curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($this->data)); // Post提交的数据包
- curl_setopt($curl, CURLOPT_TIMEOUT, self::TIMEOUT); // 设置超时限制防止死循环
- curl_setopt($curl, CURLOPT_HEADER, false); // 显示返回的Header区域内容
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // 获取的信息以文件流的形式返回
- $result = curl_exec($curl); // 执行操作
- if (curl_errno($curl)) {
- echo 'Error POST'.curl_error($curl);
- }
- curl_close($curl); // 关键CURL会话
- return $result; // 返回数据
- }
- /**
- * @param $type|提交类型 POST/GET
- * @param $isTranscoding|是否需要转 $isTranscoding 是否需要转utf-8 默认 false
- * @return mixed
- */
- public function sendSMS($type, $isTranscoding = false) {
- $this->data['content'] = $isTranscoding === true ? mb_convert_encoding($this->data['content'], "UTF-8") : $this->data['content'];
- $this->data['username'] = self::USERNAME;
- $this->data['tkey'] = date('YmdHis');
- $this->data['password'] = md5(md5(self::PASSWORD) . $this->data['tkey']);
- return $type == "POST" ? $this->httpPost() : $this->httpGet();
- }
- }
|