菜谱项目

YPSMS.php 3.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. use App\libs\sms;
  3. define("YP_SMS_KEY", "fbdb5f2ddae13c2f4a592348bfe52137");
  4. define("YP_VOICE_URL", "http://voice.yunpian.com/v2/voice/send.json");
  5. define("YP_TPL_URL", "https://sms.yunpian.com/v2/sms/tpl_single_send.json");
  6. define("YP_TPL_ID", "2262678");
  7. class YPSMS{
  8. private static function init(){
  9. $ch = curl_init();
  10. /* 设置验证方式 */
  11. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:text/plain;charset=utf-8',
  12. 'Content-Type:application/x-www-form-urlencoded', 'charset=utf-8'));
  13. /* 设置返回结果为流 */
  14. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  15. /* 设置超时时间*/
  16. curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  17. /* 设置通信方式 */
  18. curl_setopt($ch, CURLOPT_POST, 1);
  19. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  20. return $ch;
  21. }
  22. public static function sendSMS($phone,$text){
  23. $ch=self::init();
  24. // $data = array('tpl_id' => YP_TPL_ID, 'tpl_value' => ('#code#').'='.urlencode($code), 'apikey' => YP_SMS_KEY, 'mobile' => $phone);
  25. // $json_data = self::tpl_send($ch,$data);
  26. // $array = json_decode($json_data,true);
  27. // echo '<pre>';print_r($array);
  28. $data=array('tpl_id' => YP_TPL_ID,'text'=>$text,'apikey'=>YP_SMS_KEY,'mobile'=>$phone);
  29. $json_data = self::send($ch,$data);
  30. //print_r($json_data); ******************************maybe影响验证码发出
  31. // $array = json_decode($json_data,true);
  32. // echo '<pre>';print_r($array);
  33. curl_close($ch);
  34. return $json_data;
  35. }
  36. public static function sendVoiceCheck($phone,$code){
  37. $ch=self::init();
  38. $data=array('code'=>$code,'apikey'=>YP_SMS_KEY,'mobile'=>$phone);
  39. $json_data =self::voice_send($ch,$data);
  40. // $array = json_decode($json_data,true);
  41. // echo '<pre>';print_r($array);
  42. curl_close($ch);
  43. return $json_data;
  44. }
  45. private static function checkErr($result,$error) {
  46. if($result === false)
  47. {
  48. echo 'Curl error: ' . $error;
  49. }
  50. // else
  51. // {
  52. // echo '操作完成没有任何错误';
  53. // }
  54. }
  55. private static function send($ch,$data){
  56. curl_setopt ($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/sms/single_send.json');
  57. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
  58. $result = curl_exec($ch);
  59. $error = curl_error($ch);
  60. self::checkErr($result,$error);
  61. return $result;
  62. }
  63. private static function voice_send($ch,$data){
  64. curl_setopt ($ch, CURLOPT_URL, YP_VOICE_URL);
  65. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
  66. $result = curl_exec($ch);
  67. $error = curl_error($ch);
  68. self::checkErr($result,$error);
  69. return $result;
  70. }
  71. private static function tpl_send($ch,$data){
  72. curl_setopt ($ch, CURLOPT_URL, YP_TPL_URL);
  73. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
  74. $result = curl_exec($ch);
  75. $error = curl_error($ch);
  76. self::checkErr($result,$error);
  77. return $result;
  78. }
  79. }
  80. // YPSMS::init();
  81. //$code=100;
  82. //$minutes=3;
  83. // YPSMS::sendSMS('13613665865','【钱多多随手记】您的验证码是' . $code . ',有效期为' . $minutes . '分钟,请尽快验证。');
  84. // YPSMS::sendVoiceCheck('13613665865','123456');