destoon 短信发送函数及短信接口修改
// $DT在common.inc.php中定义, $CACHE = cache_read('module.php'); $DT = $CACHE['dt']; 从缓存里读取网站配置信息。
//$db 数据库类,$DT 全局配置,$DT['sms'] 开启手机短信,$DT_TIME系统时间,$DT_IP 当前ip地址,$_username当前用户
//DT_CLOUD_UID DT_CLOUD_KEY 在common.inc.php 中定义
//define('DT_CLOUD_UID', $CFG['cloud_uid']); //$CFG 加载根目录下 config.inc.php ,所以短信发送接口用户名和密码在这里设置。新版dt在 后台系统设置-》网站设置-》云服务 里设置。
//define('DT_CLOUD_KEY', $CFG['cloud_key']);
//参数:$mobile手机号,$message发送内容,$word 字数,$time 时间 function send_sms($mobile, $message, $word = 0, $time = 0) { global $db, $DT, $DT_TIME, $DT_IP, $_username; //开启短信,设置了用户名密码,手机号合法,内容给不低于5个字符。 if(!$DT['sms'] || !DT_CLOUD_UID || !DT_CLOUD_KEY || !is_mobile($mobile) || strlen($message) < 5) return false; $word or $word = word_count($message); //若没有指定字数,则通过函数word_count计算字数 ,这个函数涉及mb_strlen,strlen的区别。 $sms_message = convert($message, DT_CHARSET, 'UTF-8'); $data = 'sms_uid='.DT_CLOUD_UID.'&sms_key='.md5(DT_CLOUD_KEY.'|'.$mobile.'|'.md5 ($sms_message)).'&sms_charset='.DT_CHARSET.'&sms_mobile='.$mobile.'&sms_message='.rawurlencode($sms_message).'&sms_time='.$time; $header = "POST /send.php HTTP/1.0\r\n"; $header .= "Accept: */*\r\n"; $header .= "Content-Type: application/x-www-form-urlencoded\r\n"; $header .= "Content-Length: ".strlen($data)."\r\n\r\n"; $fp = function_exists('fsockopen') ? fsockopen('sms.destoon.com', 8820) : stream_socket_client('sms.destoon.com:8820'); $code = ''; if($fp) { fputs($fp, $header.$data); while(!feof($fp)) { $code .= fgets($fp, 1024); } fclose($fp); if($code && strpos($code, 'destoon_sms_code=') !== false) { $code = explode('destoon_sms_code=', $code); $code = $code[1]; } else { $code = 'Can Not Connect SMS Server'; } } else { $code = 'Can Not Connect SMS Server'; } //记录发送记录 $db->query("INSERT INTO {$db->pre}sms (mobile,message,word,editor,sendtime,code) VALUES ('$mobile','$message','$word','$_username','$DT_TIME','$code')"); return $code; }
//mb_strlen,strlen的区别 http://developer.51cto.com/art/201105/263103.htm
function word_count($string) { if(function_exists('mb_strlen')) return mb_strlen($string, DT_CHARSET); $string = convert($string, DT_CHARSET, 'gbk'); $length = strlen($string); $count = 0; for($i = 0; $i < $length; $i++) { $t = ord($string[$i]); if($t > 127) $i++; $count++; } return $count; }
需要注意的是,mb_strlen并不是PHP核心函数,使用前需要确保在php.ini中加载了php_mbstring.dll,即确保“extension=php_mbstring.dll”这一行存在并且没有被注释掉,否则会出现未定义函 数的问题。
修改系统默认短信端口
global.func.php send_sms函数改成
/** * 通过CURL发送HTTP请求 * @param string $url //请求URL * @param array $postFields //请求参数 * @return mixed */ function curlPost($url='',$postFields){ $postFields = http_build_query($postFields); $ch = curl_init(); curl_setopt ( $ch, CURLOPT_POST, 1 ); curl_setopt ( $ch, CURLOPT_HEADER, 0 ); curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt ( $ch, CURLOPT_URL, $url ); curl_setopt ( $ch, CURLOPT_POSTFIELDS, $postFields ); $result = curl_exec ( $ch ); curl_close ( $ch ); return $result; } /** * 发送短信 * * @param string $mobile 手机号码 * @param string $msg 短信内容 * @param string $needstatus 是否需要状态报告 * @param string $product 产品id,可选 * @param string $extno 扩展码,可选 */ function send_sms($mobile,$msg,$word = 0,$time = 0,$needstatus = 'false',$extno = '') { global $db, $DT, $DT_TIME, $DT_IP, $_username; if(!$DT['sms'] || !DT_CLOUD_UID || !DT_CLOUD_KEY || !is_mobile($mobile) || strlen($msg) <3) return false; $word or $word = word_count($message); //创蓝接口参数 $postArr = array ( 'account' => DT_CLOUD_UID, 'pswd' => DT_CLOUD_KEY, 'msg' => $msg, 'mobile' => $mobile, 'needstatus' => $needstatus, 'extno' => $extno ); $url="http://222.73.117.156/msg/HttpBatchSendSM"; $result =curlPost($url, $postArr); if($result){ $arr=explode(',',$result); $result=$arr[1]==0?1:$arr[1]; //这个接口成功返回0 返回大于1代表错误 } $db->query("INSERT INTO {$db->pre}sms (mobile,message,word,editor,sendtime,code) VALUES ('$mobile','$message','$word','$_username','$DT_TIME','$result')"); return $result; }