123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461 |
- <?php
- //阿里云短信接口
- /**
- * 生成签名并发起请求
- *
- * @param $accessKeyId string AccessKeyId (https://ak-console.aliyun.com/)
- * @param $accessKeySecret string AccessKeySecret
- * @param $domain string API接口所在域名
- * @param $params array API具体参数
- * @param $security boolean 使用https
- * @return bool|\stdClass 返回API接口调用结果,当发生错误时返回false
- */
- function aliencode($str)
- {
- $res = urlencode($str);
- $res = preg_replace("/\+/", "%20", $res);
- $res = preg_replace("/\*/", "%2A", $res);
- $res = preg_replace("/%7E/", "~", $res);
- return $res;
- }
- function alirequest($accessKeyId, $accessKeySecret, $domain, $params, $security=false) {
- $apiParams = array_merge(array (
- "SignatureMethod" => "HMAC-SHA1",
- "SignatureNonce" => uniqid(mt_rand(0,0xffff), true),
- "SignatureVersion" => "1.0",
- "AccessKeyId" => $accessKeyId,
- "Timestamp" => gmdate("Y-m-d\TH:i:s\Z"),
- "Format" => "JSON",
- ), $params);
- ksort($apiParams);
- $sortedQueryStringTmp = "";
- foreach ($apiParams as $key => $value) {
- $sortedQueryStringTmp .= "&" . aliencode($key) . "=" . aliencode($value);
- }
- $stringToSign = "GET&%2F&" . aliencode(substr($sortedQueryStringTmp, 1));
- $sign = base64_encode(hash_hmac("sha1", $stringToSign, $accessKeySecret . "&",true));
- $signature = aliencode($sign);
- $url = ($security ? 'https' : 'http')."://{$domain}/?Signature={$signature}{$sortedQueryStringTmp}";
- try {
- $content = fetchContent($url);
- return json_decode($content);
- } catch( \Exception $e) {
- return false;
- }
- }
- function fetchContent($url) {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_TIMEOUT, 5);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_HTTPHEADER, array(
- "x-sdk-client" => "php/2.0.0"
- ));
- if(substr($url, 0,5) == 'https') {
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- }
- $rtn = curl_exec($ch);
- if($rtn === false) {
- trigger_error("[CURL_" . curl_errno($ch) . "]: " . curl_error($ch), E_USER_ERROR);
- }
- curl_close($ch);
- return $rtn;
- }
- //发送短信 商城 定时脚本
- function createSmsParmsAndSend($user_mobile,$code) {
- $params = array ();
- // $code = '123456';
- // *** 需用户填写部分 ***
- // fixme 必填: 请参阅 https://ak-console.aliyun.com/ 取得您的AK信息
- $accessKeyId = "LTAIrsLAXDWeZU2h";
- $accessKeySecret = "7sYaChE8HEglWvO4yeUnRrHarNkIzL";
- // fixme 必填: 短信接收号码
- $params["PhoneNumbers"] = $user_mobile;
- // fixme 必填: 短信签名,应严格按"签名名称"填写,请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/sign
- $params["SignName"] = "小白马金服";
- // fixme 必填: 短信模板Code,应严格按"模板CODE"填写, 请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/template
- $params["TemplateCode"] = "SMS_135802079";
- // fixme 可选: 设置模板参数, 假如模板中存在变量需要替换则为必填项
- $params['TemplateParam'] = Array (
- "code" => "$code"
- );
- // fixme 可选: 设置发送短信流水号
- // $params['OutId'] = "12345";
- // fixme 可选: 上行短信扩展码, 扩展码字段控制在7位或以下,无特殊需求用户请忽略此字段
- // $params['SmsUpExtendCode'] = "1234567";
- // *** 需用户填写部分结束, 以下代码若无必要无需更改 ***
- if(!empty($params["TemplateParam"]) && is_array($params["TemplateParam"])) {
- $params["TemplateParam"] = json_encode($params["TemplateParam"], JSON_UNESCAPED_UNICODE);
- }
- // 初始化SignatureHelper实例用于设置参数,签名以及发送请求
- // $helper = new SignatureHelper();
- // 此处可能会抛出异常,注意catch
- $content = alirequest(
- $accessKeyId,
- $accessKeySecret,
- "dysmsapi.aliyuncs.com",
- array_merge($params, array(
- "RegionId" => "cn-hangzhou",
- "Action" => "SendSms",
- "Version" => "2017-05-25",
- ))
- // fixme 选填: 启用https
- // ,true
- );
- return $content;
- }
- //发送短信 商城 半小时脚本
- function createSmsAndSend($user_mobile,$code) {
- $params = array ();
- // $code = '123456';
- // *** 需用户填写部分 ***
- // fixme 必填: 请参阅 https://ak-console.aliyun.com/ 取得您的AK信息
- $accessKeyId = "LTAIrsLAXDWeZU2h";
- $accessKeySecret = "7sYaChE8HEglWvO4yeUnRrHarNkIzL";
- // fixme 必填: 短信接收号码
- $params["PhoneNumbers"] = $user_mobile;
- // fixme 必填: 短信签名,应严格按"签名名称"填写,请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/sign
- $params["SignName"] = "小白马金服";
- // fixme 必填: 短信模板Code,应严格按"模板CODE"填写, 请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/template
- $params["TemplateCode"] = "SMS_135797143";
- // fixme 可选: 设置模板参数, 假如模板中存在变量需要替换则为必填项
- $params['TemplateParam'] = Array (
- "code" => "$code"
- );
- // fixme 可选: 设置发送短信流水号
- // $params['OutId'] = "12345";
- // fixme 可选: 上行短信扩展码, 扩展码字段控制在7位或以下,无特殊需求用户请忽略此字段
- // $params['SmsUpExtendCode'] = "1234567";
- // *** 需用户填写部分结束, 以下代码若无必要无需更改 ***
- if(!empty($params["TemplateParam"]) && is_array($params["TemplateParam"])) {
- $params["TemplateParam"] = json_encode($params["TemplateParam"], JSON_UNESCAPED_UNICODE);
- }
- // 初始化SignatureHelper实例用于设置参数,签名以及发送请求
- // $helper = new SignatureHelper();
- // 此处可能会抛出异常,注意catch
- $content = alirequest(
- $accessKeyId,
- $accessKeySecret,
- "dysmsapi.aliyuncs.com",
- array_merge($params, array(
- "RegionId" => "cn-hangzhou",
- "Action" => "SendSms",
- "Version" => "2017-05-25",
- ))
- // fixme 选填: 启用https
- // ,true
- );
- return $content;
- }
- //发送短信 理财 半小时脚本
- function createSmsAndSendtwo($user_mobile,$code) {
- $params = array ();
- // $code = '123456';
- // *** 需用户填写部分 ***
- // fixme 必填: 请参阅 https://ak-console.aliyun.com/ 取得您的AK信息
- $accessKeyId = "LTAIrsLAXDWeZU2h";
- $accessKeySecret = "7sYaChE8HEglWvO4yeUnRrHarNkIzL";
- // fixme 必填: 短信接收号码
- $params["PhoneNumbers"] = $user_mobile;
- // fixme 必填: 短信签名,应严格按"签名名称"填写,请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/sign
- $params["SignName"] = "小白马金服";
- // fixme 必填: 短信模板Code,应严格按"模板CODE"填写, 请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/template
- $params["TemplateCode"] = "SMS_135792962";
- // fixme 可选: 设置模板参数, 假如模板中存在变量需要替换则为必填项
- $params['TemplateParam'] = Array (
- "code" => "$code"
- );
- // fixme 可选: 设置发送短信流水号
- // $params['OutId'] = "12345";
- // fixme 可选: 上行短信扩展码, 扩展码字段控制在7位或以下,无特殊需求用户请忽略此字段
- // $params['SmsUpExtendCode'] = "1234567";
- // *** 需用户填写部分结束, 以下代码若无必要无需更改 ***
- if(!empty($params["TemplateParam"]) && is_array($params["TemplateParam"])) {
- $params["TemplateParam"] = json_encode($params["TemplateParam"], JSON_UNESCAPED_UNICODE);
- }
- // 初始化SignatureHelper实例用于设置参数,签名以及发送请求
- // $helper = new SignatureHelper();
- // 此处可能会抛出异常,注意catch
- $content = alirequest(
- $accessKeyId,
- $accessKeySecret,
- "dysmsapi.aliyuncs.com",
- array_merge($params, array(
- "RegionId" => "cn-hangzhou",
- "Action" => "SendSms",
- "Version" => "2017-05-25",
- ))
- // fixme 选填: 启用https
- // ,true
- );
- return $content;
- }
- //发送短信 理财 定时脚本
- function createSmsParmsAndSendtwo($user_mobile,$code) {
- $params = array ();
- // $code = '123456';
- // *** 需用户填写部分 ***
- // fixme 必填: 请参阅 https://ak-console.aliyun.com/ 取得您的AK信息
- $accessKeyId = "LTAIrsLAXDWeZU2h";
- $accessKeySecret = "7sYaChE8HEglWvO4yeUnRrHarNkIzL";
- // fixme 必填: 短信接收号码
- $params["PhoneNumbers"] = $user_mobile;
- // fixme 必填: 短信签名,应严格按"签名名称"填写,请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/sign
- $params["SignName"] = "小白马金服";
- // fixme 必填: 短信模板Code,应严格按"模板CODE"填写, 请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/template
- $params["TemplateCode"] = "SMS_135807896";
- // fixme 可选: 设置模板参数, 假如模板中存在变量需要替换则为必填项
- $params['TemplateParam'] = Array (
- "code" => "$code"
- );
- // fixme 可选: 设置发送短信流水号
- // $params['OutId'] = "12345";
- // fixme 可选: 上行短信扩展码, 扩展码字段控制在7位或以下,无特殊需求用户请忽略此字段
- // $params['SmsUpExtendCode'] = "1234567";
- // *** 需用户填写部分结束, 以下代码若无必要无需更改 ***
- if(!empty($params["TemplateParam"]) && is_array($params["TemplateParam"])) {
- $params["TemplateParam"] = json_encode($params["TemplateParam"], JSON_UNESCAPED_UNICODE);
- }
- // 初始化SignatureHelper实例用于设置参数,签名以及发送请求
- // $helper = new SignatureHelper();
- // 此处可能会抛出异常,注意catch
- $content = alirequest(
- $accessKeyId,
- $accessKeySecret,
- "dysmsapi.aliyuncs.com",
- array_merge($params, array(
- "RegionId" => "cn-hangzhou",
- "Action" => "SendSms",
- "Version" => "2017-05-25",
- ))
- // fixme 选填: 启用https
- // ,true
- );
- return $content;
- }
- //发送短信 理财 定时脚本 大于100万
- function createSmsParmsAndSendbig($user_mobile,$code) {
- $params = array ();
- // $code = '123456';
- // *** 需用户填写部分 ***
- // fixme 必填: 请参阅 https://ak-console.aliyun.com/ 取得您的AK信息
- $accessKeyId = "LTAIrsLAXDWeZU2h";
- $accessKeySecret = "7sYaChE8HEglWvO4yeUnRrHarNkIzL";
- // fixme 必填: 短信接收号码
- $params["PhoneNumbers"] = $user_mobile;
- // fixme 必填: 短信签名,应严格按"签名名称"填写,请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/sign
- $params["SignName"] = "小白马金服";
- // fixme 必填: 短信模板Code,应严格按"模板CODE"填写, 请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/template
- $params["TemplateCode"] = "SMS_136160447";
- // fixme 可选: 设置模板参数, 假如模板中存在变量需要替换则为必填项
- $params['TemplateParam'] = Array (
- "code" => "$code"
- );
- // fixme 可选: 设置发送短信流水号
- // $params['OutId'] = "12345";
- // fixme 可选: 上行短信扩展码, 扩展码字段控制在7位或以下,无特殊需求用户请忽略此字段
- // $params['SmsUpExtendCode'] = "1234567";
- // *** 需用户填写部分结束, 以下代码若无必要无需更改 ***
- if(!empty($params["TemplateParam"]) && is_array($params["TemplateParam"])) {
- $params["TemplateParam"] = json_encode($params["TemplateParam"], JSON_UNESCAPED_UNICODE);
- }
- // 初始化SignatureHelper实例用于设置参数,签名以及发送请求
- // $helper = new SignatureHelper();
- // 此处可能会抛出异常,注意catch
- $content = alirequest(
- $accessKeyId,
- $accessKeySecret,
- "dysmsapi.aliyuncs.com",
- array_merge($params, array(
- "RegionId" => "cn-hangzhou",
- "Action" => "SendSms",
- "Version" => "2017-05-25",
- ))
- // fixme 选填: 启用https
- // ,true
- );
- return $content;
- }
- function foo($n ,$max = 100){
- $array = $zero = $normal = [];
- for($i=1;$i<=$n;$i++){
- $array[] = mt_rand(0,100);
- }
- $k = $max / array_sum($array); //求出放大系数k
- foreach($array as $key => $val){
- $value = floor($val * $k); //直接保留整数,以保证下一步的和肯定<100
- if($value<1){
- $zero[] = $value;
- }else{
- $normal[] = $value;
- }
- }
- $sum = array_sum($normal);
- $diff = $max - $sum; //这个值肯定<100
- if(!empty($zero)){ //如果有为0的值
- $count = count($zero);
- foreach($zero as $z){
- $normal[] = $diff / $count;
- }
- }else{ //随机分配给一个人
- $key = array_rand($normal);
- $normal[$key] = $normal[$key]+$diff;
- }
- return $normal;
- }
- function createNewProduct($data){
- $day = date('Y-m-d');
- $dayhis = date('Y-m-d H:i:s');
- //封标
- $update18 = mysql_query("UPDATE xbm_product_bid SET status=2,end_time='$dayhis' WHERE bid_id=$data[bid_id]");
- //发标
- //查询当前标的详情
- $product_bid18 = mysql_query("SELECT * FROM xbm_product_zd WHERE product_sn='$data[product_sn]'");
- //查询当前标的今天发布数量
- $todayCount18 = mysql_query("SELECT COUNT(1) AS count FROM xbm_product_bid WHERE product_sn='$data[product_sn]' AND create_time LIKE '$day%'");
- $productb18 = mysql_fetch_assoc($product_bid18);
- $todayC18 = mysql_fetch_assoc($todayCount18);
- if (($productb18['count'] - $todayC18['count']) >= 1) {
- //发短信提醒
- // $result = createSmsParmsAndSendbig(13810263268, "0018".($productb18['count'] - $todayC18['count']));
- // M('product_bid')->where("product_sn='{$sn}' AND create_time LIKE '{$day}%'")->count();
- $resRES = mysql_query("SELECT count(1) AS count FROM xbm_product_bid WHERE product_sn='$data[product_sn]' AND create_time LIKE '$day%'");
- $res =mysql_fetch_assoc($resRES);
- $resCount = $res['count']+1;
- $day18 = $data['product_sn'] . date('Ymd');
- if ($resCount) {
- $bid_sn18 = $day18 . ($resCount < 10 ? 0 . $resCount : $resCount);
- }
- //生成bid_name
- $start = strlen($data['product_sn']);
- $name = mb_substr($data['bid_name'], 0, $start);
- $name18 = mb_substr($data['bid_name'], $start);
- $name18 += 1;
- $strnum = strlen($name18);
- if ($strnum < 2) {
- $name18 = $name . str_pad($name18, 2, 0, STR_PAD_LEFT);
- }
- //生成借款人信息
- if($productb18['total_amount']/10000>=1){
- if($productb18['total_amount']/10000>1){
- $n18=rand(4,intval($productb18['total_amount']/10000));
- }else{
- $n18=$productb18['total_amount']/10000;
- }
- $money18=foo($n18,$productb18['total_amount']/1000);
- //查询对应的借款人信息
- $user_data18 = mysql_query("SELECT xbm_users_realname.user_id FROM xbm_users_realname LEFT JOIN xbm_users_phone ON xbm_users_realname.user_id=xbm_users_phone.user_id WHERE xbm_users_phone.user_id <> '' ORDER BY rand() LIMIT 0,$n18");
- //获取标的期限
- $period18 = mysql_query("SELECT period FROM xbm_product WHERE product_sn='$productb18[product_sn]' LIMIT 1");
- $period18 = mysql_fetch_assoc($period18);
- //循环插入借款人表
- $i=0;
- while($user_data182 = mysql_fetch_assoc($user_data18)){
- $borrowMoney18 = $money18[$i]*1000;
- // $user_id18 = $user_data18['user_id'];
- //插入借款人表
- $borrowInsert = mysql_query("INSERT INTO xbm_product_borrower (`borrow_time`,`user_id`,`money`,`bid_sn`,`borrow_term`,`create_time`) VALUES ('$day',$user_data182[user_id],$borrowMoney18,'$bid_sn18','$period18[period]','$dayhis')");
- $i++;
- }
- }
- //发布新标的
- $addProduct18 = mysql_query("INSERT INTO `xbm_product_bid` ( `bid_sn`, `bid_name`, `product_sn`, `current_amount`, `total_amount`, `status`, `y_status`, `send_status`, `password`, `borrow_type`, `commend`, `create_time`, `annualized_rate`, `bid_url`, `bid_content_url`)
- VALUES
- ('$bid_sn18','$name$name18','$data[product_sn]',0,$productb18[total_amount],1,0,0,NULL,0,0,'$dayhis',$productb18[annualized_rate],'','$productb18[bid_content_url]')");
- echo true;
- }elseif (($productb18['count'] - $todayCount18['count']) < 2){
- //发短信
- $result = createSmsParmsAndSendbig(13810263268,"181818");
- }
- }
|