<?php
class sms {
    function sendsms($mob, $content) {
        $msgconfig = FS("Webconfig/msgconfig");
        $type = $msgconfig['sms']['type']; // type=0 吉信通短信接口   type=1 漫道短信接口   type=2 亿美短信接口
        if ($type == 0) {
            $uid = $msgconfig['sms']['user1']; //分配给你的账号
            $pwd = $msgconfig['sms']['pass1']; //密码
            $mob = $mob; //发送号码用逗号分隔
            if (PATH_SEPARATOR == ':') { //如果是Linux系统，则执行linux短息接口
                $url = "http://service.winic.org:8009/sys_port/gateway/?id=%s&pwd=%s&to=%s&content=%s&time=";
                $id = urlencode($uid);
                $pwd = urlencode($pwd);
                $to = urlencode($mob);
                $content = iconv("UTF-8", "GB2312", $content);
                $rurl = sprintf($url, $id, $pwd, $to, $content);
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch, CURLOPT_HEADER, 0);
                curl_setopt($ch, CURLOPT_URL, $rurl);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                $result = curl_exec($ch);
                curl_close($ch);
                $status = substr($result, 0, 3);
                if ($status === "000") {
                    return true;
                } else {
                    return false;
                }
            } else {
                $content = urlencode(auto_charset($content, "utf-8", 'gbk')); //短信内容
                $sendurl = "http://service.winic.org:8009/sys_port/gateway/?";
                $sdata = "id=" . $uid . "&pwd=" . $pwd . "&to=" . $mob . "&content=" . $content . "&time=";
                $xhr = new COM("MSXML2.XMLHTTP");
                $xhr->open("POST", $sendurl, false);
                $xhr->setRequestHeader("Content-type:", "text/xml;charset=GB2312");
                $xhr->setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                $xhr->send($sdata);
                $data = explode("/", $xhr->responseText);
                if ($data[0] == "000") return true;
                else return false;
            }
        } elseif ($type == 1) {
            /////////////////////////////////////////漫道短信接口 开始/////////////////////////////////////////////////////////////
            //如果您的系统是utf-8,请转成GB2312 后，再提交、
            $flag = 0;
            //要post的数据
            $argv = array(
                'sn' => $msgconfig['sms']['user2'], ////替换成您自己的序列号
                'pwd' => $msgconfig['sms']['pass2'], //此处密码需要加密 加密方式为 md5(sn+password) 32位大写
                'mobile' => $mob, //手机号 多个用英文的逗号隔开 post理论没有长度限制.推荐群发一次小于等于10000个手机号
                'content' => iconv("UTF-8", "gb2312//IGNORE", $content) , //短信内容
                'ext' => '',
                'stime' => '', //定时时间 格式为2011-6-29 11:09:21
                'rrid' => ''
            );
            //构造要post的字符串
            foreach ($argv as $key => $value) {
                if ($flag != 0) {
                    $params.= "&";
                    $flag = 1;
                }
                $params.= $key . "=";
                $params.= urlencode($value);
                $flag = 1;
            }
            $length = strlen($params);
            //创建socket连接
            $fp = fsockopen("sdk2.zucp.net", 8060, $errno, $errstr, 10) or exit($errstr . "--->" . $errno);
            //构造post请求的头
            $header = "POST /webservice.asmx/mt HTTP/1.1\r\n";
            $header.= "Host:sdk2.zucp.net\r\n";
            $header.= "Content-Type: application/x-www-form-urlencoded\r\n";
            $header.= "Content-Length: " . $length . "\r\n";
            $header.= "Connection: Close\r\n\r\n";
            //添加post的字符串
            $header.= $params . "\r\n";
            //发送post的数据
            fputs($fp, $header);
            $inheader = 1;
            while (!feof($fp)) {
                $line = fgets($fp, 1024); //去除请求包的头只显示页面的返回数据
                if ($inheader && ($line == "\n" || $line == "\r\n")) {
                    $inheader = 0;
                }
                if ($inheader == 0) {
                    // echo $line;
                    
                }
            }
            $line = str_replace("
<string xmlns=\"http://tempuri.org/\">", "", $line);
            $line = str_replace("</string>
", "", $line);
            $result = explode("-", $line);
            if (count($result) > 1) {
                return false;
            } else {
                return true;
            }
            /////////////////////////////////////////漫道短信接口 结束/////////////////////////////////////////////////////////////
            
        } elseif ($type == 2) {
            ////////////////////////////////////////////////////////亿美短信接口 开始/////////////////////////////////////////////
            $uid = $msgconfig['sms']['user3']; //分配给你的账号
            $pwd = $msgconfig['sms']['pass3']; //密码
            $mob = $mob; //发送号码用逗号分隔
            $content = urlencode(auto_charset($content, "utf-8", 'gbk')); //短信内容
            $sendurl = "http://sdk229ws.eucp.b2m.cn:8080/sdkproxy/sendsms.action?";
            $sendurl.= 'cdkey=' . $serialNumber . '&password=' . $pwd . '&phone=' . $mob . '&message=' . $content . '&addserial=';
            $d = @file_get_contents($sendurl, false);
            preg_match_all('/
<response>
(.*)<\/response>/isU', $d, $arr);
            foreach ($arr[1] as $k => $v) {
                preg_match_all('#
<error>(.*)</error>
#isU', $v, $ar[$k]);
                $data[] = $ar[$k][1];
            }
            if ($data[0][0] == "0") {
                return true;
            } else {
                return false;
            }
            ////////////////////////////////////////////////////////亿美短信接口 结束/////////////////////////////////////////////
            
        } else {
            return false;
        }
    }
}
?>

