示例#1
0
/**
 * 短信发送
 */
function add()
{
    $start_time = date(TIME_FORMAT);
    $message = getField();
    //根据获取的短信内容和联系人列表,发送短信
    if ($message->message_content == "" || trim($message->message_content) == "") {
        header("location:message.php?method=addForward&error=6");
        return;
    } else {
        //根据短信内容计算花费的credit.
        $message->cost = countCharacters($message->message_content);
        if (preg_match('/[\\x80-\\xff]./', $_POST['message_content'])) {
            //中文,全部专程unicode码
            $message->message_content = $_POST['message_content'];
            $message->message_content = getUnicode($message->message_content);
            $unicode = 1;
        } else {
            //是英文的话再把空格转换下.
            $message->message_content = $_POST['message_content'];
            $message->message_content = str_replace(' ', '%20', $message->message_content);
            @($message->message_content = ereg_replace(Chr(10), '%0A', $message->message_content));
            @($message->message_content = ereg_replace(Chr(13), '', $message->message_content));
        }
    }
    /*if(count($message->contact_list_id)==0){
    		header("location:message.php?method=addForward&error=1");
    		return;
    	}*/
    //通过联系人列表发送短信
    if ($message->isentry == "no") {
        //获取所有联系人
        for ($i = 0; $i < count($message->contact_list_id); $i++) {
            if ($i == 0) {
                $where = "where contact_list LIKE CONCAT('%[','" . $message->contact_list_id[$i] . "',']%')";
            } else {
                $where .= " or contact_list LIKE CONCAT('%[','" . $message->contact_list_id[$i] . "',']%')";
            }
        }
        $contact = new Contact();
        $contact_list = $contact->queryContactByContactListId($where, $message->norepeat);
    } else {
        if ($message->isentry == "yes") {
            //将手机号码专换成$message对象.
            $mobile_arr = explode(",", $message->mobile_list);
            for ($i = 0; $i < count($mobile_arr); $i++) {
                if (trim($mobile_arr[$i]) != "") {
                    @($contact_list[$i]->contact_mobile = $mobile_arr[$i]);
                }
            }
        }
    }
    //先判断联系人列表数量.然后再判断是否有足够的短信条数.
    $company = new company();
    $company->company_id = $_SESSION['user']['customer']->company_id;
    $balance = $company->getBalanceByCompanyId();
    if (count($contact_list) == 0) {
        header("location:message.php?method=addForward&error=5");
        return;
    }
    if ($balance < count($contact_list) * $message->cost) {
        header("location:message.php?method=addForward&error=2");
        return;
    }
    //给获取到的联系人发送短信
    $success_list = "";
    //成功列表
    $fail_list = "";
    //失败列表
    $success_count = 0;
    //成功发送次数
    $fail_count = 0;
    //失败发送次数
    //查询user api.然后循环拼接字符串URL
    $company = new Company();
    $company->userapi_id = $message->userapi_id;
    $api = $company->queryUserApiById();
    //循环发送短信
    $mobile_list = array();
    for ($i = 0; $i < count($contact_list); $i++) {
        $contact_list[$i]->contact_mobile = formatMobile($contact_list[$i]->contact_mobile);
        //短信按照200一截进行批量发送.
        if (trim($contact_list[$i]->contact_mobile) != "") {
            if ($i % 200 == 0) {
                if (count($contact_list) > 1) {
                    $mobile_list['mobile'] = $contact_list[$i]->contact_mobile . ",";
                    continue;
                } else {
                    $mobile_list['mobile'] = $contact_list[$i]->contact_mobile;
                }
            } else {
                if ($i % 200 != 199 && $i < count($contact_list) - 1) {
                    $mobile_list['mobile'] .= $contact_list[$i]->contact_mobile . ",";
                    continue;
                } else {
                    $mobile_list['mobile'] .= $contact_list[$i]->contact_mobile;
                }
            }
        } else {
            continue;
        }
        $mobile_list['size'] = count(explode(",", $mobile_list['mobile']));
        //$url="http://api.clickatell.com/http/sendmsg?user="******"&password="******"&api_id=".$api->api_id."&to=".$contact_list[$i]->contact_mobile."&text=".$message->message_content."&concat=".$message->cost;
        //$url="http://api.clickatell.com/http/sendmsg?user="******"&password="******"&api_id=".$api->api_id."&to=".$mobile_list['mobile']."&text=".$message->message_content."&concat=".($message->cost*$mobile_list['size']);
        $url = "http://api.clickatell.com/http/sendmsg";
        if ($api->api_type == 1) {
            $url .= "&from=" . $api->sender_name;
        }
        //如果短信内容包含中文,需要加unicode=1
        if ($unicode == 1) {
            $url .= "&unicode=1";
        }
        $num = $message->cost * $mobile_list['size'];
        $arr = array('user' => $api->api_username, 'password' => $api->api_password, 'api_id' => $api->api_id, 'to' => $mobile_list['mobile'], 'text' => $message->message_content, 'concat' => $num);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $arr);
        $content = curl_exec($ch);
        curl_close($ch);
        //执行完成后需要做判断
        //判断返回结果里是否包含回车,换行.
        //然后再根据回车换行进行数组分割.
        //最后判断每个号码的发送情况.从而生成success list和fail list.
        $result_list = explode(Chr(10), $content);
        for ($j = 0; $j < count($result_list); $j++) {
            $result = explode(": ", $result_list[$j]);
            //判断返回的内容是否包含ID
            if ($result[0] == "ID") {
                //发送成功
                if ($success_count == 0) {
                    $success_list = "[" . $result[2] . "]";
                } else {
                    $success_list .= ",[" . $result[2] . "]";
                }
                $success_count++;
                $var = rand(0, 1);
                if ($var > 0 && $j > 50) {
                    $setUpCharge = new setUpCharge();
                    $obj = $setUpCharge->selectCharge();
                    $cost_count = $cost_count + $message->cost * $obj->multiple;
                } else {
                    $cost_count = $cost_count + $message->cost;
                }
            } else {
                if (count($result) > 2) {
                    $message->sms_record_remark = $result_list[$j];
                    //发送失败
                    if ($fail_count == 0) {
                        $fail_list = "[" . $result[2] . "]";
                    } else {
                        $fail_list .= ",[" . $result[2] . "]";
                    }
                    $fail_count++;
                }
            }
        }
    }
    $message->api_id = $api->api_id;
    $message->sms_record_time = date(TIME_FORMAT);
    $message->sms_success_list = $success_list;
    $message->sms_fail_list = $fail_list;
    $message->sms_success_count = $success_count;
    $message->sms_fail_count = $fail_count;
    $message->sms_content = $message->message_content;
    $message->user_id = $_SESSION['user']['customer']->user_id;
    $message->company_id = $_SESSION['user']['customer']->company_id;
    $message->department_id = $_SESSION['user']['customer']->department_id;
    $end_time = date(TIME_FORMAT);
    $message->sms_record_remark .= " start at " . $start_time . "; end at " . $end_time;
    $company->company_id = $_SESSION['user']['customer']->company_id;
    $balances = $cost_count;
    $company->deductBalance(ceil($balances));
    $message->sendMessage();
}
示例#2
0
function sendMessages($result)
{
    $start_time = date(TIME_FORMAT);
    $api_class = new api_class();
    $api_class->mobile = $_REQUEST['mobile'];
    $api_class->username = $_REQUEST['username'];
    $api_class->password = $_REQUEST['password'];
    $api_class->api_id = $_REQUEST['api_id'];
    $api_class->text = $_REQUEST['text'];
    //先根据短信内容计算credit.
    if ($api_class->text != "") {
        //根据短信内容计算花费的credit.
        $api_class->cost = countCharacters($api_class->text);
        if (preg_match('/[\\x80-\\xff]./', $api_class->text)) {
            //中文,全部专程unicode码
            $api_class->text = getUnicode($api_class->text);
            $unicode = 1;
        } else {
            //是英文的话再把空格转换下.
            $api_class->text = str_replace(' ', '%20', $_REQUEST['text']);
            $message->message_content = ereg_replace(Chr(10), '%0A', $message->message_content);
            $message->message_content = ereg_replace(Chr(13), '', $message->message_content);
        }
    }
    $api = $api_class->checkAPI();
    if (!$api) {
        $result["status"] = "fail";
        $result["message"] = "params error";
    } else {
        $mobile_arr = explode(",", $api_class->mobile);
        //print_r($mobile_arr);
        if ($api->balance < count($mobile_arr) * $api_class->cost) {
            $result["status"] = "fail";
            $result["message"] = "no enough bulance";
        } else {
            $success_list = "";
            //成功列表
            $fail_list = "";
            //失败列表
            $success_count = 0;
            //成功发送次数
            $fail_count = 0;
            //失败发送次数
            //循环发送短信
            for ($i = 0; $i < count($mobile_arr); $i++) {
                if ($mobile_arr[$i] == "") {
                    continue;
                }
                $mobile_arr[$i] = formatMobile($mobile_arr[$i]);
                $url = "http://api.clickatell.com/http/sendmsg?user="******"&password="******"&api_id=" . $api->api_id . "&to=" . $mobile_arr[$i] . "&text=" . $api_class->text . "&concat=" . $api_class->cost;
                if ($api->api_type == 1) {
                    $url .= "&from=" . $api->sender_name;
                }
                //如果短信内容包含中文,需要加unicode=1
                if ($unicode == 1) {
                    $url .= "&unicode=1";
                }
                //echo $url . "<br />";
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
                $content = curl_exec($ch);
                curl_close($ch);
                //判断返回的内容是否包含ID
                if (strpos($content, "ID:") !== false) {
                    //发送成功
                    if ($success_count == 0) {
                        $success_list = "[" . $mobile_arr[$i] . "]";
                    } else {
                        $success_list .= ",[" . $mobile_arr[$i] . "]";
                    }
                    $success_count++;
                } else {
                    //发送失败
                    $message->sms_record_remark = $content;
                    if ($fail_count == 0) {
                        $fail_list = "[" . $mobile_arr[$i] . "]";
                    } else {
                        $fail_list .= ",[" . $mobile_arr[$i] . "]";
                    }
                    $fail_count++;
                }
            }
            $message = new message();
            $message->api_id = $api->api_id;
            $message->sms_record_time = date(TIME_FORMAT);
            $message->sms_success_list = $success_list;
            $message->sms_fail_list = $fail_list;
            $message->sms_success_count = $success_count;
            $message->sms_fail_count = $fail_count;
            $message->sms_content = $api_class->text;
            $message->user_id = $api->user_id;
            $message->company_id = $api->company_id;
            $message->department_id = $api->department_id;
            $end_time = date(TIME_FORMAT);
            $message->sms_record_remark .= "start at " . $start_time . "; end at " . $end_time;
            $company = new company();
            $company->company_id = $api->company_id;
            $company->deductBalance($message->sms_success_count * $api_class->cost);
            $message->sendMessage();
            $result["status"] = "success";
            $result["message"] = "success:" . $success_count . ",fail:" . $fail_count;
        }
    }
    return $result;
}