Sms.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace tencent;
  3. /*
  4. Name:FastPHP
  5. CopyRight: Mr.Fu 2019SR0832915
  6. */
  7. /*
  8. 腾讯短信接口插件
  9. 调用方法:
  10. // $Sms=new \app\extend\tencent\Sms;
  11. // $data[0]=mt_rand(1000,9999);
  12. // echo $Sms->SendSms('13477191977',$data,'330371');
  13. // echo $Sms->SendVoiceSms('13477191977','4564',336953);
  14. */
  15. // use \app\base\Common;
  16. class Sms
  17. {
  18. private $AppID="";
  19. private $AppKey="";
  20. private $sign="";
  21. public function __construct()
  22. {
  23. $config = sysconfig('sms');
  24. if(!empty($config)){
  25. // 短信总开关
  26. if(1!=$config['send_switch']){
  27. return false;
  28. }else{
  29. $this->AppID=$config['tencent_sms_appid'];
  30. $this->AppKey=$config['tencent_sms_key'];
  31. $this->sign=$config['tencent_sms_sign'];
  32. }
  33. }
  34. }
  35. //发送短信
  36. public function SendSms($mobile,$params,$tpl_id)
  37. {
  38. $random=mt_rand(100000,999999);
  39. $URL='https://yun.tim.qq.com/v5/tlssmssvr/sendsms?sdkappid='.$this->AppID.'&random='.$random;
  40. $time=time();
  41. $string='appkey='.$this->AppKey.'&random='.$random.'&time='.$time.'&mobile='.$mobile;
  42. $sign=hash("sha256",$string);
  43. $data=array("ext"=>"","extend"=> "","params"=> $params,"sig"=>$sign,"sign"=>$this->sign,"tel"=>array("mobile"=>$mobile,'nationcode'=>86),"time"=>$time,"tpl_id"=>$tpl_id);
  44. $aContext = array('http' => array('method' => 'POST', 'header' => 'Content-type: application/json', 'content' => json_encode($data) ));
  45. $cxContext = stream_context_create($aContext);
  46. $d_json = @file_get_contents($URL,false,$cxContext);
  47. $d = json_decode($d_json,true);
  48. WLog('tencent_sms_log',$d_json);
  49. if(isset($d['errmsg']) && $d['errmsg']=='OK'){
  50. return $d;
  51. }else{
  52. return $d;
  53. }
  54. }
  55. //发送模板语音短信
  56. public function SendVoiceSms($mobile,$params,$tpl_id)
  57. {
  58. $random=mt_rand(100000,999999);
  59. $URL='https://cloud.tim.qq.com/v5/tlsvoicesvr/sendtvoice?sdkappid='.$this->AppID.'&random='.$random;
  60. $time=time();
  61. $string='appkey='.$this->AppKey.'&random='.$random.'&time='.$time.'&mobile='.$mobile;
  62. $sign=hash("sha256",$string);
  63. $json='{
  64. "tpl_id": '.$tpl_id.',
  65. "params": [ "'.$params.'" ],
  66. "playtimes":2,
  67. "sig": "'.$sign.'",
  68. "tel": {
  69. "mobile": "'.$mobile.'",
  70. "nationcode": "86"
  71. },
  72. "time": '.$time.',
  73. "ext": ""
  74. }';
  75. $aContext = array('http' => array('method' => 'POST', 'header' => 'Content-type: application/json', 'content' => $json));
  76. $cxContext = stream_context_create($aContext);
  77. $d_json = @file_get_contents($URL,false,$cxContext);
  78. $d = json_decode($d_json,true);
  79. WLog('tencent_voice_sms_log',$d_json);
  80. if($d['errmsg']=='OK'){
  81. return $d;
  82. }else{
  83. return false;
  84. }
  85. }
  86. }
  87. ?>