'3210114', //新订单审核提醒管理员 //2 => '3210120', //新订单提醒仓管 2 => '3211800', //新订单提醒仓管 3 => '3210134', //订单发货提醒销售 4 => '3210146', //订单审核通过提醒销售 5 => '3210312', //订单审核驳回提醒销售 6 => '3391172', //redis预占库存异常报警 ]; private static function init(){ $ch = curl_init(); /* 设置验证方式 */ curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:text/plain;charset=utf-8', 'Content-Type:application/x-www-form-urlencoded', 'charset=utf-8')); /* 设置返回结果为流 */ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); /* 设置超时时间*/ curl_setopt($ch, CURLOPT_TIMEOUT, 10); /* 设置通信方式 */ curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); return $ch; } public static function sendMsg($phone,$type,$order_id = null, $send_date = null){ $tpl = self::$order_tpl; $tpl_id = $tpl[$type]; $ch=self::init(); //$data=array('tpl_id' => $tpl_id,'text'=>$text,'apikey'=>YP_SMS_KEY,'mobile'=>$phone); $data = [ 'apikey' => YP_SMS_KEY_1, 'mobile' => $phone, 'tpl_id' => $tpl_id, ]; if( in_array($type, [3,5]) ){ $data['tpl_value'] = ('#order_id#').'='.$order_id; }elseif( $type == 2){ $data['tpl_value'] = ('#order_id#').'='.$order_id.'&'. ('#send_date#').'='.urldecode($send_date); } $json_data = self::tpl_send($ch,$data); //print_r($json_data); ******************************maybe影响验证码发出 $array = json_decode($json_data,true); // echo '
';print_r($array);
        curl_close($ch);
        return $array;
    }

    //预占库存报警
    public static function sendSkuWnum($phone, $note = null){
        $tpl = self::$order_tpl;
        $tpl_id = $tpl[6];
        $ch=self::init();       
        $data = [
                'apikey' => YP_SMS_KEY_1,
                'mobile' => $phone,
                'tpl_id' => $tpl_id,
                ];
        
        $data['tpl_value'] = ('#note#').'='.$note;
        $json_data = self::tpl_send($ch,$data);
        $array = json_decode($json_data,true);
        curl_close($ch);
        return $array;
    }

    public static function sendSMS($phone,$text){
        $ch=self::init();

        // $data = array('tpl_id' => YP_TPL_ID, 'tpl_value' => ('#code#').'='.urlencode($code), 'apikey' => YP_SMS_KEY, 'mobile' => $phone);
        // $json_data = self::tpl_send($ch,$data);
        // $array = json_decode($json_data,true);
        // echo '
';print_r($array);
        $data=array('tpl_id' => YP_TPL_ID,'text'=>$text,'apikey'=>YP_SMS_KEY_1,'mobile'=>$phone);

        $json_data = self::send($ch,$data);
        //print_r($json_data);    ******************************maybe影响验证码发出
         $array = json_decode($json_data,true);
        // echo '
';print_r($array);
        curl_close($ch);
        return $array;
    }	

    public static function sendRedSMS($phone,$text,$id){
        $ch=self::init();
        $data=array('tpl_id' => $id,'text'=>$text,'apikey'=>YP_SMS_YHQ,'mobile'=>$phone);
        $json_data = self::send($ch,$data);
        curl_close($ch);
        return $json_data;
    }

    public static function sendVoiceCheck($phone,$code){
        $ch=self::init();
        $data=array('code'=>$code,'apikey'=>YP_SMS_KEY_1,'mobile'=>$phone);
        $json_data =self::voice_send($ch,$data);
        // $array = json_decode($json_data,true);
        // echo '
';print_r($array);
        curl_close($ch);
        return $json_data;

    }
    private static function checkErr($result,$error) {
        if($result === false)
        {
            echo 'Curl error: ' . $error;
        }
//        else
//        {
//            echo '操作完成没有任何错误';
//        }
    }
    private static function send($ch,$data){
        curl_setopt ($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/sms/single_send.json');
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        $result = curl_exec($ch);
        $error = curl_error($ch);
        self::checkErr($result,$error);
        return $result;
    }
    private static function voice_send($ch,$data){
        curl_setopt ($ch, CURLOPT_URL, YP_VOICE_URL);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        $result = curl_exec($ch);
        $error = curl_error($ch);
        self::checkErr($result,$error);
        return $result;
    }
    private static function tpl_send($ch,$data){
        curl_setopt ($ch, CURLOPT_URL, YP_TPL_URL_1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        $result = curl_exec($ch);
        $error = curl_error($ch);
        self::checkErr($result,$error);
        return $result;
    }
}
// YPSMS::init();
//$code=100;
//$minutes=3;
// YPSMS::sendSMS('13613665865','【钱多多随手记】您的验证码是' . $code . ',有效期为' . $minutes . '分钟,请尽快验证。');
// YPSMS::sendVoiceCheck('13613665865','123456');