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; } }