1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- <?php
- use App\libs\sms;
- define("YP_SMS_KEY", "fbdb5f2ddae13c2f4a592348bfe52137");
- define("YP_VOICE_URL", "http://voice.yunpian.com/v2/voice/send.json");
- define("YP_TPL_URL", "https://sms.yunpian.com/v2/sms/tpl_single_send.json");
- define("YP_TPL_ID", "2262678");
- class YPSMS{
- 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 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 '<pre>';print_r($array);
- $data=array('tpl_id' => YP_TPL_ID,'text'=>$text,'apikey'=>YP_SMS_KEY,'mobile'=>$phone);
- $json_data = self::send($ch,$data);
- //print_r($json_data); ******************************maybe影响验证码发出
- // $array = json_decode($json_data,true);
- // echo '<pre>';print_r($array);
- curl_close($ch);
- return $json_data;
- }
- public static function sendVoiceCheck($phone,$code){
- $ch=self::init();
- $data=array('code'=>$code,'apikey'=>YP_SMS_KEY,'mobile'=>$phone);
- $json_data =self::voice_send($ch,$data);
- // $array = json_decode($json_data,true);
- // echo '<pre>';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);
- 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');
|