123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Administrator
- * Date: 2015/12/24
- * Time: 16:56
- */
- namespace common\library;
- use common\models\Shortmsg;
- /**
- * 短信接口
- * Class LMMessage
- * @package common\libary
- */
- class LMMessage {
- const LIMIT_SECOND = 60; //同个号码发送频率
- // static function SendMessage($mobiles,$content, $limit= true){
- //
- // if($limit && !self::limitTime($mobiles)){
- // return false;
- // }
- // if(empty($mobiles) && !self::mobileFormat($mobiles)){
- // return false;
- // }
- //
- // $cust_code = \Yii::$app->params['message']['id'];
- // $password =\Yii::$app->params['message']['pwd'];
- //// $sp_code = '';
- // $destMobiles = $mobiles;
- //
- // $url = "http://".\Yii::$app->params['message']['ip'].":8860/";
- //
- // $post_data = array();
- // $post_data['cust_code'] = $cust_code;
- // $post_data['destMobiles'] = $destMobiles;
- // $post_data['content'] = $content;
- // $post_data['sign'] = md5(urlencode($content.$password)); //签名
- //
- // $o="";
- // foreach ($post_data as $k=>$v)
- // {
- // $o.= "$k=".urlencode($v)."&";
- // }
- // $post_data=substr($o,0,-1);
- // $ch = curl_init();
- // curl_setopt($ch, CURLOPT_POST, 1);
- // curl_setopt($ch, CURLOPT_HEADER, 0);
- // curl_setopt($ch, CURLOPT_URL,$url);
- // //为了支持cookie
- // curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
- // curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
- // curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
- // $result = curl_exec($ch);
- //
- // $resultarr = self::resultHandle($result);
- // self::saveShortMessage($destMobiles,$content,$resultarr);
- // return $resultarr;
- // }
- /**
- * 新短信接口接入
- * @param $mobiles
- * @param $content
- * @param bool $limit
- * @return bool
- */
- static function SendMessage($mobiles,$content, $limit= true){
- if($limit && !self::limitTime($mobiles)){
- return false;
- }
- if(empty($mobiles) && !self::mobileFormat($mobiles)){
- return false;
- }
- $cust_code = \Yii::$app->params['message']['id'];
- $password =\Yii::$app->params['message']['pwd'];
- // $sp_code = '';
- $destMobiles = $mobiles;
- $url = "http://".\Yii::$app->params['message']['ip'].":8860/";
- $post_data = array();
- $post_data['cust_code'] = $cust_code;
- $post_data['content'] = $content;
- $post_data['destMobiles'] = $destMobiles;
- $post_data['sign'] =md5(urlencode($content.$password)); //签名
- $o="";
- foreach ($post_data as $k=>$v)
- {
- $o.= "$k=".urlencode($v)."&";
- }
- $post_data=substr($o,0,-1);
- ;
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_setopt($ch, CURLOPT_URL,$url);
- //为了支持cookie
- // curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
- curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
- $result = curl_exec($ch);
- $resultarr = self::resultHandle($result);
- self::saveShortMessage($destMobiles,$content,$resultarr);
- return $resultarr;
- }
- static function saveShortMessage($destMobiles,$content,$resultarr){
- $short = new Shortmsg();
- $short->msgid = isset($resultarr[0]['msgid'])?$resultarr[0]['msgid']:"";
- $short->recipient = $destMobiles;
- $short->content = $content;
- $short->receipt = isset($resultarr[0]['state'])?$resultarr[0]['state']:"失败";
- $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){
- $reg = '/[\s]+/s';
- $result = preg_split($reg,$result);
- $resultarr = [];
- foreach($result as $k => $v){
- if($k != 0){
- $ar = mb_split(':',$v);
- if(count($ar) == 3){
- $resultarr[] = ['mobile'=>$ar[0],'msgid'=>$ar[1],'state'=>$ar[2]];
- }
- }
- }
- return $resultarr;
- }
- /**
- *接收下行的回执
- * @param $include
- * @return bool
- */
- static function receiveState($include){
- return false;
- }
- /**
- * 接收上行消息
- * @param $include
- * @return bool
- */
- static function receiveMessage($include){
- return false;
- }
- /**
- * 获取账号余额
- */
- static function getMoney(){
- $result= '';
- return $result;
- }
- }
|